lintcode: Balanced Binary Tree
来源:互联网 发布:java修改文件权限 编辑:程序博客网 时间:2024/06/06 04:41
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.
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */class Solution {public: /** * @param root: The root of binary tree. * @return: True if this Binary tree is Balanced, or false. */ int height(TreeNode *node){ if(node==NULL){ return 0; } return max(height(node->left),height(node->right))+1; } bool isBalanced(TreeNode *root) { // write your code here if(root==NULL){ return true; } int diff=height(root->left)-height(root->right); if(diff>1||diff<-1){ return false; } return isBalanced(root->left) && isBalanced(root->right); }};
参考:
http://www.cnblogs.com/infinityu/archive/2013/05/11/3073411.html
https://haozhou.gitbooks.io/leetcode-java/content/binarytree/binarytree-balanced.html
0 0
- lintcode: Balanced Binary Tree
- 20150624 lintcode 总结 Balanced Binary Tree
- lintcode balanced-binary-tree 平衡二叉树
- lintcode 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
- 自定义view之一:自定义验证码控件
- QQ也可以和微信一样只能共同好友见评论
- matlab图像数据转换函数
- 问题 A: 输入字符串以及输出
- 123
- lintcode: Balanced Binary Tree
- ACM——GCD算法
- mybatis 中#与$的区别
- SwingWeb
- 获取类的属性、方法等
- bnuoj 51124 Simple String Problem(状态dp)
- Java学习笔记-虚拟机类加载机制
- Dom对象的常用方法:
- 2016_03_20_整理