当前位置:网站首页>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
}
边栏推荐
- InputStream to inputstreamsource
- MySQL日志管理、数据备份、恢复
- 拼多多店铺搜索相关问题,为什么新品上架搜索不到
- UE4_ Editor UMG close window cannot destroy UMG immediately
- Turn off automatic outlining in Visual Studio - turning off automatic outlining in Visual Studio
- Acwing winter vacation daily question 2022 punch in day 11
- 领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
- 2022年,谁在推动音视频产业的新拐点?
- Using lazy < t > in C # to realize singleton mode in WPF
- Configure the user to log in to the device through telnet -- AAA local authentication
猜你喜欢

STM32F103系列控制的OLED IIC 4针

Gestion des utilisateurs de la base de données MySQL

Golang之手写web框架

What are membrane stress and membrane strain

Here comes the nearest chance to Ali

Solidy - fallback function - 2 trigger execution modes

Database SQL language 03 sorting and paging

Cisco vxlan configuration

从零开发 stylelint规则(插件)

STM32F103 series controlled OLED IIC 4-pin
随机推荐
抓取手机端变体组合思路设想
You don't know how to deduce the location where HashSet stores elements?
[road of system analyst] collection of wrong topics in Project Management Chapter
Switch to software testing and report to the training class for 3 months. It's a high paying job. Is it reliable?
AI大模型落地大考,浪潮交出了怎样的答卷?
1380. lucky numbers in matrices
How does WPS cancel automatic numbering? Four options
Transfer the token on the matic-erc20 network to the matic polygon
Simple use of qlistview of QT (including source code + comments)
/Path/to/ idiom, not a command
Qt之QListView的简单使用(含源码+注释)
Mysql database user management
At the age of 32, I fell into a middle-aged crisis and finally quit naked...
We strongly recommend more than a dozen necessary plug-ins for idea development
OpenCL线程代数库ViennaCL的使用
MySQL 索引
Official win 10 image download
Summary of redis learning notes (I)
C语言基础小操作
[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point