Leetcode110. Balanced Binary Tree
来源:互联网 发布:深圳美工师到哪考 编辑:程序博客网 时间:2024/06/08 11:09
110. Balanced Binary Tree
1、原题
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 of every node never differ by more than 1.
2、题意与解析
这道题目的意思很简单,就是让我们判断一个二叉树是不是一个平衡二叉树。
在这里我的思路是通过递归判断每一层的左右自树的高度。进行判断。具体看代码。
3、代码
int getHeight(TreeNode* root) {if (root == null) {return 0;}//求左右子树的高度 int l = getHeight(root->left);int r = getHeight(root->right);//当其子树中存在不平衡的部分或左右子树不平衡 if (abs(l - r) > 1 || l == -2 || r == -2) {return -2;}return max(l,r) + 1;}bool isBalanced(TreeNode* root) {if (root == null) { return true; } return getHeight(root) != -2;}
阅读全文
0 0
- LeetCode110 - Balanced Binary Tree
- LeetCode110-Balanced Binary Tree
- leetcode110---Balanced Binary Tree
- LeetCode110:Balanced Binary Tree
- LeetCode110. Balanced Binary Tree
- leetcode110~Balanced Binary Tree
- leetcode110. Balanced Binary Tree
- LeetCode110 Balanced Binary Tree
- leetcode110 Balanced Binary Tree
- Leetcode110. Balanced Binary Tree
- LeetCode110. Balanced Binary Tree
- LeetCode110—Balanced Binary Tree
- LeetCode110.Balanced Binary Tree题解
- LeetCode110——Balanced Binary Tree
- LeetCode110 Blanced Binary Tree Java 题解
- Balanced binary tree 扩展
- LeetCode: Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- js 中的this
- Eclipse常用快捷
- java方法是传值还是传引用问题
- ogre 世界坐标、本地坐标、屏幕坐标之间的转换
- HDU1083 Courses(二分图匹配)
- Leetcode110. Balanced Binary Tree
- [vim] 删除
- 类名.class, class.forName(), getClass()区别
- 修改mysql默认字符集为utf
- Hadoop HA 环境搭建
- android 6.0(api 23) SDK,不再提供org.apache.http.*(只保留几个类)
- JAVA读取模板,向Linux服务器写入html文件
- Mybatis级联删除、修改、查询问题
- 源码:我的关于NLP的博客(持续更新中...)