[LeetCode] Balanced Binary Tree

来源:互联网 发布:贵州大数据先天优势是 编辑:程序博客网 时间:2024/06/08 07:53
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;    }}

0 0