当前位置:网站首页>【剑指 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);
}
}
};
边栏推荐
- [development of large e-commerce projects] performance pressure test - basic concept of pressure test & jmeter-38
- Simple two ball loading
- Machine learning summary (I): linear regression, ridge regression, Lasso regression
- 1553B环境搭建
- China NdYAG crystal market research conclusion and development strategy proposal report Ⓥ 2022 ~ 2028
- 9. Use of better scroll and ref
- The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise
- Investment analysis and prospect prediction report of global and Chinese p-nitrotoluene industry Ⓙ 2022 ~ 2027
- 啟動solr報錯The stack size specified is too small,Specify at least 328k
- Example code of second kill based on MySQL optimistic lock
猜你喜欢

终端识别技术和管理技术

Jenkins+webhooks- multi branch parametric construction-

Professor Li Zexiang, Hong Kong University of science and technology: I'm wrong. Why is engineering consciousness more important than the best university?
Example code of second kill based on MySQL optimistic lock

Computer network interview knowledge points

详细讲解面试的 IO多路复用,select,poll,epoll

MySQL六十六问,两万字+五十图详解!复习必备

6年技术迭代,阿里全球化出海&合规的挑战和探索
![[241. Design priority for operation expression]](/img/72/29d27204d5213a8efdb2c5be925dec.png)
[241. Design priority for operation expression]

Understand the window query function of tdengine in one article
随机推荐
Report on the "14th five year plan" and scale prospect prediction of China's laser processing equipment manufacturing industry Ⓢ 2022 ~ 2028
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?
Colorful five pointed star SVG dynamic web page background JS special effect
Declare an abstract class vehicle, which contains the private variable numofwheel and the public functions vehicle (int), horn (), setnumofwheel (int) and getnumofwheel (). Subclass mot
Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
Machine learning summary (I): linear regression, ridge regression, Lasso regression
Camp division of common PLC programming software
MySQL Replication中的并行复制示例详解
2.15 summary
Detailed explanation of parallel replication examples in MySQL replication
[安网杯 2021] REV WP
运行游戏时出现0xc000007b错误的解决方法[通俗易懂]
一款Flutter版的记事本
ArrayList扩容机制以及线程安全性
Judea pearl, Turing prize winner: 19 causal inference papers worth reading recently
Arthas use
String input function
Asp.netcore利用dynamic简化数据库访问
MySQL 66 questions, 20000 words + 50 pictures in detail! Necessary for review
Detailed explanation of leetcode reconstruction binary tree [easy to understand]