Question:
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
思路:首先平衡树的判断标准是:树上的任意结点的左右子树高度差不超过1,则为平衡二叉树。
那么写一个递归函数,记录二叉树结点的左子树高度h1和右子树高度h2,若出现|h1-h2|>1则该二叉树不是平衡二叉树。
Answer:
/**
* 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:
int flag=true;
int dfs(TreeNode *root)
{
if(root==NULL)
return true;
int h1,h2;
if(root->left==NULL)
h1=0;
else
h1=dfs(root->left);
if(root->right==NULL)
h2=0;
else
h2=dfs(root->right);
if(abs(h1-h2)>1)
flag=0;
return max(h1,h2)+1;
}
bool isBalanced(TreeNode *root) {
dfs(root);
return flag;
}
};