110. Balanced Binary Tree
2017-09-13 10:51
176 查看
/* 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 for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isBalanced(TreeNode* root) { if(!root) return true; return depth(root)!=-1; } int depth(TreeNode* root) { if(!root) return 0; int leftdepth=depth(root->left); if(leftdepth==-1) return -1; int rightdepth=depth(root->right); if(rightdepth==-1) return -1; if(abs(leftdepth-rightdepth)>1) return -1; return max(leftdepth,rightdepth)+1; } };
相关文章推荐
- Leetcode 110. Balanced Binary Tree (Easy) (cpp)
- 15算法课程 110. Balanced Binary Tree
- LeetCode110.Balanced Binary Tree题解
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- [leetcode]110. Balanced Binary Tree
- LeetCode解题报告 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- LeetCode笔记:110. Balanced Binary Tree
- 110.Balanced Binary Tree Leetcode解题笔记
- [leetCode By Python]110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 【LeetCode】 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- leetcode-110. Balanced Binary Tree
- [LeetCode]110. Balanced Binary Tree