int height(TreeNode *root) {
if(root == NULL) {
return 0;
}
int left_height = height(root->left);
int right_height = height(root->right);
return left_height > right_height? (left_height+1) :(right_height+1);
}
bool isBalanced(TreeNode *root) {
if(root == NULL) {
return true;
}
if(isBalanced(root->left) && isBalanced(root->right)
&& abs(height(root->left)-height(root->right)) <= 1) {
return true;
}
else {
return false;
}
}
[LeetCode] Balanced Binary Tree
最新推荐文章于 2017-06-28 16:04:50 发布