/**
* 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 height(TreeNode* root) {
if(root==NULL)
return 0;
else{
int l=height(root->left);
int r=height(root->right);
return 1+((l>r)?l:r);
}
}
bool isBalanced(TreeNode *root) {
if(root==NULL)
return true;
else{
int l,r;
l=height(root->left);
r=height(root->right);
if((l>r+1)||(r>l+1))
return false;
else
return isBalanced(root->left)&&isBalanced(root->right);
}
}
};
LeetCode(110)Balanced Binary Tree
最新推荐文章于 2018-12-26 22:25:37 发布