Balanced Binary Tree
来源:互联网 发布:MAC 百度网盘 编辑:程序博客网 时间:2024/05/16 05:06
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.
用递归来解,对于每一层检验是否为平衡的,如果当前某个子树不平衡,则直接返回不平衡,否则返回当前的深度值,用-1来标记不平衡。class Solution {public: bool isBalanced(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function return (cal_depth(root) >= 0)?true:false; } int cal_depth(TreeNode *root) { if(root == NULL) return 0; int left_depth = cal_depth(root->left) ; if(left_depth < 0) return -1; int right_depth = cal_depth(root->right); if(right_depth < 0) return -1; if(abs(left_depth - right_depth) > 1) return -1; return 1+max(left_depth,right_depth); }};64 milli secs
- 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
- [leetcode]Balanced Binary Tree
- 伙伴系统分析
- Android开发之四(十四):常用控件之菜单(Menu)
- sharepoint 2010 控制台应用程序 找不到位于 http://xxx 的 Web 应用程序
- JAVA内存溢出 解决方案
- 解析HTML5 Web Forms 2.0 新特性
- Balanced Binary Tree
- Unknown error: java.lang.NullPointerException.
- Overrides the OnInitDialog function, Windows Title, Icon
- 如何进行网站的广告数据分析
- .net后台POST传值
- Linux下的TCP通信
- Magento去掉底部recent viewed和best seller
- 用java判断数据库某个表是否存在
- STK / Connect 模块分析与应用