当前位置:网站首页>[Jianzhi offer] 55 - ii balanced binary tree
[Jianzhi offer] 55 - ii balanced binary tree
2022-07-01 13:39:00 【LuZhouShiLi】
The finger of the sword Offer 55 - II. Balanced binary trees
subject
Enter the root node of a binary tree , Judge whether the tree is a balanced binary tree . If the depth difference between the left and right subtrees of any node in a binary tree does not exceed 1, So it's a balanced binary tree .
Ideas
First, define a function to calculate the height of nodes , Then traverse according to the preorder of the binary tree , For the node currently traversed , First, calculate the height of the left and right subtrees , If the height difference between the left and right subtrees does not exceed 1, Recursively traverse the left and right child nodes respectively , And judge whether the left and right subtrees are balanced .
Code
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */
class Solution {
public:
int height(TreeNode* root)
{
if(root == NULL)
{
return 0;
}
else
{
// Calculate the height of the binary tree
return max(height(root->left),height(root->right)) + 1;
}
}
bool isBalanced(TreeNode* root)
{
if(root == NULL)
{
return true;
}
else
{
return abs(height(root->left) - height(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right);
}
}
};
边栏推荐
- 机器学习总结(一):线性回归、岭回归、Lasso回归
- Asp. NETCORE uses dynamic to simplify database access
- Example code of second kill based on MySQL optimistic lock
- 2022 · 让我带你Jetpack架构组件从入门到精通 — Lifecycle
- [安网杯 2021] REV WP
- 焱融看 | 混合云时代下,如何制定多云策略
- Analysis report on the development prospect and investment strategy of the global and Chinese laser chip industry Ⓑ 2022 ~ 2027
- Flow management technology
- ArrayList扩容机制以及线程安全性
- 开源者的自我修养|为 ShardingSphere 贡献了千万行代码的程序员,后来当了 CEO
猜你喜欢

When you really learn databinding, you will find "this thing is really fragrant"!

Explain IO multiplexing, select, poll, epoll in detail

9. Use of better scroll and ref

6年技术迭代,阿里全球化出海&合规的挑战和探索

【241. 为运算表达式设计优先级】

SAP 智能机器人流程自动化(iRPA)解决方案分享

刘对(火线安全)-多云环境的风险发现

IO的几种模型 阻塞,非阻塞,io多路复用,信号驱动和异步io

A Fletter version of Notepad
MySQL报错1040Too many connections的原因以及解决方案
随机推荐
arthas使用
leetcode 322. Coin Change 零钱兑换(中等)
Apache-atlas-2.2.0 independent compilation and deployment
5. Use of ly tab plug-in of header component
Report on the 14th five year plan and future development trend of China's integrated circuit packaging industry Ⓓ 2022 ~ 2028
Computer network interview knowledge points
[machine learning] VAE variational self encoder learning notes
MySQL六十六问,两万字+五十图详解!复习必备
Explain IO multiplexing, select, poll, epoll in detail
龙蜥社区开源 coolbpf,BPF 程序开发效率提升百倍
Anti fraud, refusing to gamble, safe payment | there are many online investment scams, so it's impossible to make money like this
MySQL报错1040Too many connections的原因以及解决方案
Self cultivation of open source programmers who contributed tens of millions of lines of code to shardingsphere and later became CEO
6年技术迭代,阿里全球化出海&合规的挑战和探索
The 14th five year plan of China's environmental protection industry and the report on the long-term goals for 2035 Ⓖ 2022 ~ 2028
Three questions about scientific entrepreneurship: timing, pain points and important decisions
Detailed explanation of leetcode reconstruction binary tree [easy to understand]
7. Icons
北斗通信模块 北斗gps模块 北斗通信终端DTU
Reasons for MySQL reporting 1040too many connections and Solutions