Balanced Binary Tree
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(!root)
return true;
return depth(root)>0;
}
int depth(TreeNode* root){
if(!root)
return 0;
int l=depth(root->left), r=depth(root->right);
if(l<0||r<0||abs(l-r)>1) return -1;//剪枝 every node!!!!!!!!!!!!!!!!!!!!!!!!!!!!
return 1+max(l,r);
}
};
Last updated
Was this helpful?