当前位置:网站首页>【剑指 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);
}
}
};
边栏推荐
- When you really learn databinding, you will find "this thing is really fragrant"!
- 2022 · 让我带你Jetpack架构组件从入门到精通 — Lifecycle
- A Fletter version of Notepad
- Investment analysis and prospect prediction report of global and Chinese dimethyl sulfoxide industry Ⓦ 2022 ~ 2028
- Jenkins+webhooks- multi branch parametric construction-
- Analysis report on the development trend and Prospect of new ceramic materials in the world and China Ⓐ 2022 ~ 2027
- Wave animation color five pointed star loader loading JS special effects
- 研发效能度量框架解读
- Cs5268 advantages replace ag9321mcq typec multi in one docking station scheme
- Blind box NFT digital collection platform system development (build source code)
猜你喜欢

Flow management technology

What is the future development direction of people with ordinary education, appearance and family background? The career planning after 00 has been made clear

The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise

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

学历、长相、家境普通的人,未来的发展方向是什么?00后的职业规划都已经整得明明白白......

Chen Yu (Aqua) - Safety - & gt; Cloud Security - & gt; Multicloud security

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

JS discolored Lego building blocks

5G工业网关的科技治超应用 超限超重超速非现场联合执法

面试题目总结(1) https中间人攻击,ConcurrentHashMap的原理 ,serialVersionUID常量,redis单线程,
随机推荐
Asp.netcore利用dynamic简化数据库访问
Terminal identification technology and management technology
minimum spanning tree
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?
Declare an abstract class vehicle, which contains the private variable numofwheel and the public functions vehicle (int), horn (), setnumofwheel (int) and getnumofwheel (). Subclass mot
JVM有哪些类加载机制?
[安网杯 2021] REV WP
ArrayList capacity expansion mechanism and thread safety
北斗通信模块 北斗gps模块 北斗通信终端DTU
ArrayList扩容机制以及线程安全性
Beidou communication module Beidou GPS module Beidou communication terminal DTU
Who should I know when opening a stock account? Is it actually safe to open an account online?
2022 · 让我带你Jetpack架构组件从入门到精通 — Lifecycle
Blind box NFT digital collection platform system development (build source code)
Machine learning - performance metrics
04-Redis源码数据结构之字典
启动solr报错The stack size specified is too small,Specify at least 328k
[development of large e-commerce projects] performance pressure test - basic concept of pressure test & jmeter-38
2022上半年英特尔有哪些“硬核创新”?看这张图就知道了!
JS discolored Lego building blocks