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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.