leetcode 110 : 平衡二叉树
题目
算法思想 :我们只需要递归的判断每个根节点的左右子树高度是否小于等于1即可。
int maxDepth(TreeNode* root) {
int high = 0;
if(root == NULL)
high = 0;
else
high = max(maxDepth(root->left),maxDepth(root->right) ) + 1;
return high;
}
bool isBalanced(TreeNode* root) {
if(root == NULL)
return true;
int l_h = maxDepth(root->left);
int r_h = maxDepth(root->right);
if(abs(l_h-r_h) > 1)
return false;
else
return (isBalanced(root->left) && isBalanced(root->right) );
return true;
}