Balanced Binary Tree
2017-03-01 19:11
90 查看
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.
class Solution { public: bool isBalanced(TreeNode* root) { if(!root) return true; if(abs(getDepth(root->left)-getDepth(root->right))>1) return false; if(!isBalanced(root->left)||!isBalanced(root->right)) return false; return true; } int getDepth(TreeNode* root){ if(root==NULL) return 0; //if(root->left||root->right) return 1+max(getDepth(root->left),getDepth(root->right)); //return 1; } };
相关文章推荐
- leetcode 110 Balanced Binary Tree(DFS)
- LeetCode:Balanced Binary Tree
- [leetcode]Balanced Binary Tree @ Python
- balanced binary tree
- [LeetCode]104. Maximum Depth of Binary Tree&110. Balanced Binary Tree
- LeetCode--Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- leetcode110 Balanced Binary Tree
- [LeetCode]--110. Balanced Binary Tree
- 110. Balanced Binary Tree
- leetcode 110. Balanced Binary Tree(平衡二叉树的判断) DFS深度优先遍历
- 【leetcode】Balanced Binary Tree
- 平衡二叉树(Balanced Binary Tree)
- 110. Balanced Binary Tree
- Leetcode::Balanced Binary Tree
- *LeetCode-Balanced Binary Tree
- 二叉树递归问题 leetcode 572. Subtree of Another Tree & leetcode 110. Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- Leetcode110. Balanced Binary Tree
- Balanced Binary Tree(Java)