题目描述

输入一棵二叉树,判断该二叉树是否是平衡二叉树。

分析:采用后序遍历的方式判断左右子树的高度差是否大于1

class Solution {
public:
bool flag;
int f(TreeNode* root)
{
    if(root)
    {
         //后序遍历,从下网往上,每个结点只遍历一次
           int x=f(root->left);
        int y=f(root->right);
        
        if(abs(x-y)>1)
        {
            flag=false;
        }else
        {
            return max(x,y)+1;
        }
    }else
    {
        return 0;
    }
    return 0;
}
bool IsBalanced_Solution(TreeNode* pRoot)
{
    flag=true;
    f(pRoot);
    return flag;
}
};

相关文章:

  • 2021-10-30
  • 2021-08-10
  • 2021-07-20
  • 2022-01-10
  • 2022-12-23
  • 2021-06-25
  • 2021-11-20
  • 2021-07-23
猜你喜欢
  • 2021-11-29
  • 2022-02-27
  • 2021-07-23
  • 2022-12-23
  • 2021-08-28
相关资源
相似解决方案