Balanced Binary Tree
bool isBalanced(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int depth;
return helper(root,depth);
}
bool helper(TreeNode* root,int& depth)
{
if(!root)
{
depth = 0;
return true;
}
int ld,rd;
bool bBalance = helper(root->left,ld)&&helper(root->right,rd);
if(abs(ld-rd)>1)
bBalance = false;
depth = max(ld,rd)+1;
return bBalance;
}
浙公网安备 33010602011771号