4.1-判断二叉树是否平衡(same in LeetCode)
2014-08-06 16:05
465 查看
Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ in distance from the root by more than one.
判断二叉树是否平衡。
leetcode: http://blog.csdn.net/todorovchen/article/details/18675329
int calBf(TreeNode* root, bool& bf)
{
if(root==NULL)
return 0;
int leftHeight = calBf(root->left, bf) + 1;
int rightHeight = calBf(root->right, bf) + 1;
if(abs(rightHeight-leftHeight)>1)
{
bf=false;
return -1;
}
return max(leftHeight, rightHeight);
}
bool isBalanced(TreeNode *root)
{
bool bf = true;
calBf(root, bf);
return bf;
}
判断二叉树是否平衡。
leetcode: http://blog.csdn.net/todorovchen/article/details/18675329
int calBf(TreeNode* root, bool& bf)
{
if(root==NULL)
return 0;
int leftHeight = calBf(root->left, bf) + 1;
int rightHeight = calBf(root->right, bf) + 1;
if(abs(rightHeight-leftHeight)>1)
{
bf=false;
return -1;
}
return max(leftHeight, rightHeight);
}
bool isBalanced(TreeNode *root)
{
bool bf = true;
calBf(root, bf);
return bf;
}
相关文章推荐
- LeetCode基础--二叉树-判断二叉树是否平衡
- Leetcode Same Tree 判断两棵二叉树是否相同
- 【LeetCode101-110】二叉树对称及存储,前序中序遍历生成二叉树,中序后序生成二叉树,数组转化为AVL平衡树,判断二叉树是否平衡
- 判断二叉树是否平衡(Bottom-up)
- 【leetcode 二叉树平衡判断】
- 剑指offer解题报告(Java版)——二叉树的深度 判断二叉树是否平衡 38
- 判断给定二叉树是否是二叉搜索树(LeetCode: Validate Binary Search Tree)
- 判断一个二叉树是否是平衡二叉树 Cracking the coding interview 4.1
- 判断二叉树是否平衡以及二叉树的路径和
- 判断二叉树是否平衡
- [leetcode]Same Tree(判断两个二叉树是否相等 C语言实现)
- 4.4-按行输出二叉树(same in LeetCode)
- 【leetcode】比较两棵二叉树是否相同(Same Tree)
- 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现
- LeetCode刷题之三:判断两个二叉树是否相同
- 判断二叉树是否平衡、是否完全二叉树、是否二叉排序树
- LeetCode(Symmetric Tree)判断二叉树是否是对称的
- 判断二叉树是否平衡
- Leetcode-Symmetric Tree——判断二叉树是否对称
- [LeetCode] Symmetric Tree 判断二叉树是否为对称二叉树