[leetcode]110. Balanced Binary Tree
Analysis
组会又被怼了—— [哎~]
Given a binary tree, determine if it is height-balanced.
判断一个二叉树是否是平衡二叉树~
这里详细介绍了平衡二叉树的判断: https://www.geeksforgeeks.org/how-to-determine-if-a-binary-tree-is-balanced/
Implement
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
int lh, rh;
if(root == NULL)
return true;
lh = height(root->left);
rh = height(root->right);
if(abs(lh-rh)<=1 && isBalanced(root->left) && isBalanced(root->right))
return true;
return false;
}
int height(TreeNode* root){
if(root == NULL)
return 0;
return max(height(root->left), height(root->right))+1;
}
};