当前位置:网站首页>[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);
}
}
};
边栏推荐
- Liu Dui (fire line safety) - risk discovery in cloudy environment
- 用命令行 给 apk 签名
- ArrayList扩容机制以及线程安全性
- Report on the 14th five year plan and future development trend of China's integrated circuit packaging industry Ⓓ 2022 ~ 2028
- Summary of interview questions (1) HTTPS man in the middle attack, the principle of concurrenthashmap, serialVersionUID constant, redis single thread,
- 6. Wiper part
- How much money do novices prepare to play futures? Is agricultural products OK?
- Beidou communication module Beidou GPS module Beidou communication terminal DTU
- Cs5268 advantages replace ag9321mcq typec multi in one docking station scheme
- Learning to use livedata and ViewModel will make it easier for you to write business
猜你喜欢

流量管理技术
MySQL报错1040Too many connections的原因以及解决方案

学会使用LiveData和ViewModel,我相信会让你在写业务时变得轻松

Colorful five pointed star SVG dynamic web page background JS special effect

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

陈宇(Aqua)-安全-&gt;云安全-&gt;多云安全
Reasons for MySQL reporting 1040too many connections and Solutions

Liu Dui (fire line safety) - risk discovery in cloudy environment

AnimeSR:可学习的降质算子与新的真实世界动漫VSR数据集
基于mysql乐观锁实现秒杀的示例代码
随机推荐
Word2vec training Chinese word vector
JS discolored Lego building blocks
刘对(火线安全)-多云环境的风险发现
终端识别技术和管理技术
Some summary of pyqt5 learning (overview of the general meaning of some signals and methods)
The 14th five year plan of China's environmental protection industry and the report on the long-term goals for 2035 Ⓖ 2022 ~ 2028
Global and Chinese polypropylene industry prospect analysis and market demand forecast report Ⓝ 2022 ~ 2027
Analysis report on the development trend and Prospect of new ceramic materials in the world and China Ⓐ 2022 ~ 2027
机器学习总结(一):线性回归、岭回归、Lasso回归
内容审计技术
spark源码阅读总纲
Use of shutter SQLite
Machine learning summary (I): linear regression, ridge regression, Lasso regression
Liu Dui (fire line safety) - risk discovery in cloudy environment
Cs5268 advantages replace ag9321mcq typec multi in one docking station scheme
Summary of 20 practical typescript single line codes
Collation and review of knowledge points of Microcomputer Principle and interface technology - pure manual
French Data Protection Agency: using Google Analytics or violating gdpr
运行游戏时出现0xc000007b错误的解决方法[通俗易懂]
Spark source code (V) how does dagscheduler taskscheduler cooperate with submitting tasks, and what is the corresponding relationship between application, job, stage, taskset, and task?