110. Balanced Binary Tree
来源:互联网 发布:熊猫智能采集软件 编辑:程序博客网 时间:2024/05/01 17:26
题目描述:
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofevery node never differ by more than 1.
思路详解:
设置一个高度h与平衡因子balance,如果平衡,则为1,如果不平衡,则为0.如果节点为空,则为h=0,balance = 1;如果节点无孩子 ,h=1,balance = 1;
再向上比较balance即可。
答案详解:
/**
* 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:
void judge(TreeNode* root, int &balance,int &h)
{
int br,bl,hr,hl;
if(root==NULL)
{
balance=1;
h=0;
}
else if(root->left==NULL&&root->right==NULL)
{
balance=1;
h=1;
}
else
{
judge(root->left,bl,hl);
judge(root->right,br,hr);
h=(hr > hl ? hr : hl)+1;
if(abs(hr-hl)<2)
balance=bl&br;
else
balance=0;
}
}
bool isBalanced(TreeNode* root) {
int balance,h;
judge(root,balance,h);
return balance;
}
};
阅读全文
0 0
- 110.Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- MFC对话框控件的属性、方法、消息
- Spring MVC运行原理
- Java-Collection源码分析(十二)——SortedMap和NavigableMap
- 学习笔记-Large scale machine learning
- LINUX定时执行SHELL脚本实现DB2数据库操作,crontab无法正常识别执行
- 110. Balanced Binary Tree
- JZOJ5390. 【NOIP2017提高A组模拟9.26】逗气
- linux 下常用命令
- RBAC权限管理
- javascript数组查重
- 固定表格布局
- Ubuntu安装Hadoop
- Uncaught ReferenceError: $ is not defined
- 测试下项目,帮大佬转发下