leetcode_question_110 Balanced Binary Tree
来源:互联网 发布:js返回顶部过渡 效果 编辑:程序博客网 时间:2024/04/29 13:57
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 everynode never differ by more than 1.
Recursive:
bool balance(TreeNode* root, int &depth) { if(root == NULL) {depth = 0;return true;}int leftdepth = 0;bool left = balance(root->left, leftdepth);int rightdepth = 0;bool right = balance(root->right, rightdepth);depth = leftdepth > rightdepth ? leftdepth+1 : rightdepth+1;int gap = leftdepth - rightdepth;return left && right && (-1 <= gap && gap <= 1);}bool isBalanced(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function int depth = 0;return balance(root, depth); }
- leetcode_question_110 Balanced Binary Tree
- Balanced binary tree 扩展
- LeetCode: Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [Leetcode] Balanced Binary Tree
- LeetCode : Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- Balanced Binary Tree
- Balanced Binary Tree
- 【leetcode】Balanced Binary Tree
- Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- Balanced Binary Tree
- Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- [Leetcode]Balanced Binary Tree
- [Android] Android开发优化之——对界面UI的优化(3)
- Eclipse快捷键大全
- HDOJ, 杭电2049, 不容易系列之(4)——考新郎。。。组合加错排。
- getchar()与EOF总结
- 高性能浏览器网络(High Performance Browser Networking) 第三章
- leetcode_question_110 Balanced Binary Tree
- 【JAVA io】学习记录
- ios7 UIAlertView 不能加自己View
- 2013 ACM/ICPC Asia Regional Changsha Online - C Color Representation Conversion
- 赛马问题
- 17,标准C
- OpenMP 与 MPICH2 进行集群计算时数据分发与共享
- iOS7之Sprite Kit
- 2014阿里巴巴校园招聘笔试题-----研发工程师