当前位置:网站首页>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
}
边栏推荐
- MySQL storage system
- How to print pthread_ t - How to print pthread_ t
- English grammar_ Adjective / adverb Level 3 - superlative
- [secretly kill little partner pytorch20 days] - [day4] - [example of time series data modeling process]
- Configure the user to log in to the device through telnet -- AAA local authentication
- VLAN access mode
- [ansible series] fundamentals 02 module debug
- 09- [istio] istio service entry
- Delete the repeating elements in the sorting list (simple questions)
- Idea of capturing mobile terminal variant combination
猜你喜欢

Rotating frame target detection mmrotate v0.3.1 training dota data set (II)
![[secretly kill little partner pytorch20 days] - [day4] - [example of time series data modeling process]](/img/f3/e51cb80f838faba8dfd90596d6e57b.jpg)
[secretly kill little partner pytorch20 days] - [day4] - [example of time series data modeling process]

聲網,站在物聯網的“土壤”裏

Dynamic programming -- gliding wing of the strange thief Kidd
![[GPU] basic operation](/img/76/6b22368e3addd30aef1dd2258ee49a.jpg)
[GPU] basic operation

The average salary of software testing in 2022 has been released. Have you been averaged?

谁不想要一个自己的博客网站呢 - 搭建博客网站wordpress

1380. lucky numbers in matrices

2022年,谁在推动音视频产业的新拐点?

MySQL存储系统
随机推荐
Configure the user to log in to the device through telnet -- AAA local authentication
[Blue Bridge Road -- bug free code] DS1302 time module code analysis
Sword finger offer 29 Print matrix clockwise
Xi'an Jiaotong 21st autumn "computerized accounting" online homework answer sheet (I) [standard answer]
Qt之QListView的简单使用(含源码+注释)
MySQL數據庫用戶管理
Dao -- a beautiful new world?
Create uiactionsheet [duplicate] - creating uiactionsheet [duplicate]
Database SQL language 04 subquery and grouping function
二十四、输入输出设备模型(串口/键盘/磁盘/打印机/总线/中断控制器/DMA和GPU)
Cisco vxlan configuration
聲網,站在物聯網的“土壤”裏
We strongly recommend more than a dozen necessary plug-ins for idea development
English grammar_ Adjective / adverb Level 3 - superlative
How to automatically renew a token after it expires?
Switch to software testing and report to the training class for 3 months. It's a high paying job. Is it reliable?
[GPU] basic operation
D. Big Brush
Here comes the nearest chance to Ali
【LeetCode】236. Nearest common ancestor of binary tree