当前位置:网站首页>Balanced binary tree judgment of Li Kou 110 -- classic problems
Balanced binary tree judgment of Li Kou 110 -- classic problems
2022-06-30 05:55:00 【Ape Bridge】
Title Overview :
Ideas : Beyond all doubt , Most of the basic topics about binary trees must use the idea of recursion . This topic is no exception .
Here we use the idea of postorder traversal , First traverse the left subtree , And then I go through the right subtree , Final judgment root . Every time you traverse , We all have to find the depth of the root node corresponding to the left and right subtrees , Then make a comparison , The absolute value is less than 2 Then this node meets , Traverse in turn , It is not a balanced tree until all nodes are satisfied .
In this code, we must first write a code to find the depth of the binary tree , It's very simple. I won't go into details here .
The code is as follows :
int maxDepth(struct TreeNode* root)
{
if(root==NULL)
{
return 0;
}
int leftdepth=maxDepth(root->left);
int rightdepth=maxDepth(root->right);
return leftdepth>rightdepth?leftdepth+1:rightdepth+1;
}
bool isBalanced(struct TreeNode* root)
{
if(root==NULL)
{
return true;
}
return abs(maxDepth(root->left)-maxDepth(root->right))<2&&
isBalanced(root->left)&&isBalanced(root->right); // This expression means that each node must return true Finally, I came back true
}
边栏推荐
- What indicators should safety service engineers pay attention to in emergency response?
- MySQL數據庫用戶管理
- Solitidy - fallback 回退函数 - 2种触发执行方式
- Solidity - 安全 - 重入攻击(Reentrancy)
- 强烈推荐十几款IDEA开发必备的插件
- [chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point
- Database SQL language 06 single line function
- Solidity - Security - reentrancy attack
- What do you think of the deleted chat records? How to restore the deleted chat records on wechat?
- Do you know how to show the health code in only 2 steps
猜你喜欢

MySQL存储系统

Sword finger offer 22 The penultimate node in the linked list
![[GPU] basic operation](/img/76/6b22368e3addd30aef1dd2258ee49a.jpg)
[GPU] basic operation

What do you think of the deleted chat records? How to restore the deleted chat records on wechat?

2022年,谁在推动音视频产业的新拐点?
![[Alibaba cloud] student growth plan answers](/img/34/cba975c0960d5595433adcb23f6e64.jpg)
[Alibaba cloud] student growth plan answers

OSPF - authentication and load balancing summary (including configuration commands)

Shenzhou ares tx6 boot logo modification tutorial

MySQL advanced (Advanced SQL statement)

inno setup 最简单的自定义界面效果
随机推荐
AI大模型落地大考,浪潮交出了怎样的答卷?
Rotating frame target detection mmrotate v0.3.1 training dota data set (II)
谁不想要一个自己的博客网站呢 - 搭建博客网站wordpress
[Alibaba cloud] student growth plan answers
1380. lucky numbers in matrices
D. Big Brush
[ansible series] fundamentals -01
Create uiactionsheet [duplicate] - creating uiactionsheet [duplicate]
Learning automation ppt
UE4_ Editor UMG close window cannot destroy UMG immediately
Shenzhou ares tx6 boot logo modification tutorial
Tornado frame foundation
MySQL存储系统
Xijiao 21 autumn "motor and drive" online homework answer sheet (I) [standard answer]
Delete the repeating elements in the sorting list (simple questions)
Huxiaochun came to fengshu electronics to sign a strategic cooperation agreement with Zoomlion
Database SQL language 06 single line function
Create priority queue
Attempt to redefine 'timeout' at line 2 solution
Video summary of my station B