【LeetCode】Balanced Binary Tree
2014-04-14 22:46
274 查看
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.
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 for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int GetHeight(TreeNode *node, bool &balanced) { if(node == NULL) return -1; int i = GetHeight(node->left, balanced); int j = GetHeight(node->right, balanced); if(abs(i-j) > 1) balanced = false; return max(i, j) + 1; } bool isBalanced(TreeNode *root) { bool balanced = true; int i = GetHeight(root, balanced); return balanced; } };
相关文章推荐
- LeetCode 110.Balanced Binary Tree
- [LeetCode] 110. Balanced Binary Tree 平衡二叉树
- 【LeetCode笔记】Balanced Binary Tree 高度平衡二叉树
- LeetCode110:Balanced Binary Tree
- leetcode -- Balanced Binary Tree -- 简单重点
- leetCode(20):Balanced binary tree
- [leetCode By Python]110. Balanced Binary Tree
- LeetCode--No.110--Balanced Binary Tree--待完善
- Leetcode-标签为Tree 110. Balanced Binary Tree
- [Leetcode]Balanced Binary Tree
- LeetCode-110.Balanced Binary Tree
- [LeetCode][二叉树]Balanced Binary Tree
- LeetCode——Balanced Binary Tree
- leetcode_110_Balanced Binary Tree
- [Leetcode] Balanced Binary Tree
- Leetcode Balanced Binary Tree
- Balanced Binary Tree -- LeetCode
- [LeetCode] Balanced Binary Tree Solution
- [leetcode][tree][dfs] Balanced Binary Tree
- LeetCode(24)-Balanced Binary Tree