当前位置:网站首页>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
}
边栏推荐
- Uboot reads the DDR memory size by sending 'R' characters through the terminal
- Force deduction exercise -- deleting repeated items in ordered sequence 1.0
- Codeforces C. Andrew and Stones
- 聲網,站在物聯網的“土壤”裏
- Lantern Festival | maoqiu technology and everyone "guess riddles and have a good night"
- Sound net, debout dans le "sol" de l'IOT
- I have been working as a software testing engineer for 5 years, but I was replaced by an intern. How can I improve myself?
- Solitidy - fallback 回退函数 - 2种触发执行方式
- 强烈推荐十几款IDEA开发必备的插件
- 领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
猜你喜欢

1380. lucky numbers in matrices

Sound network, standing in the "soil" of the Internet of things

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

Using lazy < t > in C # to realize singleton mode in WPF
![[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]
![[Alibaba cloud] student growth plan answers](/img/34/cba975c0960d5595433adcb23f6e64.jpg)
[Alibaba cloud] student growth plan answers

剑指 Offer 18. 删除链表的节点

MySQL storage system

Configure the user to log in to the device through telnet -- AAA local authentication

Sword finger offer 18 Delete the node of the linked list
随机推荐
二十四、输入输出设备模型(串口/键盘/磁盘/打印机/总线/中断控制器/DMA和GPU)
Turn off automatic outlining in Visual Studio - turning off automatic outlining in Visual Studio
Xctf attack and defense world crypto advanced area
Intelligent deodorizer embedded development
领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
Basic operations of C language
[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point
SSL证书续费相关问题详解
inno setup 最简单的自定义界面效果
Finally someone can make the server so straightforward
What are membrane stress and membrane strain
超简单 STM32 RTC闹钟 时钟配置
PC viewing WiFi password
8 ways to earn passive income
炒股用指南针开户交易安全吗?
OpenCL线程代数库ViennaCL的使用
抓取手机端变体组合思路设想
Stack overflow caused by C # using protobuf stack overflow
I have been working as a software testing engineer for 5 years, but I was replaced by an intern. How can I improve myself?
Delete the repeating elements in the sorting list (simple questions)