当前位置:网站首页>【剑指 Offer】55 - II. 平衡二叉树
【剑指 Offer】55 - II. 平衡二叉树
2022-07-01 13:26:00 【LuZhouShiLi】
剑指 Offer 55 - II. 平衡二叉树
题目
输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。
思路
首先定义一个计算节点高度的函数,然后根据二叉树的前序遍历,对于当前遍历的节点,首先计算左右子树的高度,如果左右子树的高度差是否不超过1,在分别递归遍历左右子节点,并判断左右子树是否平衡。
代码
/** * 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
{
// 计算二叉树的高度
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);
}
}
};
边栏推荐
- About fossage 2.0 "meta force meta universe system development logic scheme (details)
- The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise
- Simple two ball loading
- MySQL报错1040Too many connections的原因以及解决方案
- Google Earth engine (GEE) - Global Human Settlements grid data 1975-1990-2000-2014 (p2016)
- 5. Use of ly tab plug-in of header component
- Research Report on China's software outsourcing industry investment strategy and the 14th five year plan Ⓡ 2022 ~ 2028
- Analysis report on the development trend and prospect scale of silicon intermediary industry in the world and China Ⓩ 2022 ~ 2027
- Global and Chinese n-butanol acetic acid market development trend and prospect forecast report Ⓧ 2022 ~ 2028
- Analysis report on the development prospect and investment strategy of the global and Chinese laser chip industry Ⓑ 2022 ~ 2027
猜你喜欢

5. Use of ly tab plug-in of header component

开源者的自我修养|为 ShardingSphere 贡献了千万行代码的程序员,后来当了 CEO

04 redis source code data structure dictionary

La taille de la pile spécifiée est petite, spécifiée à la sortie 328k

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

Cs5268 advantages replace ag9321mcq typec multi in one docking station scheme

终端识别技术和管理技术

8 popular recommended style layout

Svg diamond style code

Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
随机推荐
Apache-atlas-2.2.0 independent compilation and deployment
孔松(信通院)-数字化时代云安全能力建设及趋势
7. Icons
MySQL 66 questions, 20000 words + 50 pictures in detail! Necessary for review
Analysis report on the development trend and prospect scale of silicon intermediary industry in the world and China Ⓩ 2022 ~ 2027
arthas使用
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?
Asp.netcore利用dynamic简化数据库访问
During Oracle CDC data transmission, the CLOB type field will lose its value during update. There is a value before update, but
Report on the 14th five year plan and future development trend of China's integrated circuit packaging industry Ⓓ 2022 ~ 2028
波浪动画彩色五角星loader加载js特效
1. Sum of two numbers: given an integer array num and an integer target value, please find the two integers whose sum is the target value target in the array and return their array subscripts
Qtdeisgner, pyuic detailed use tutorial interface and function logic separation (nanny teaching)
Computer network interview knowledge points
受益互联网出海 汇量科技业绩重回高增长
Router. use() requires a middleware function but got a Object
Introduction to topological sorting
Flutter SQLite使用
Wave animation color five pointed star loader loading JS special effects
10. Page layout, guess you like it