首页 > 试题广场 >

判断是不是平衡二叉树

[编程题]判断是不是平衡二叉树
  • 热度指数:532799 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
输入一棵节点数为 n 二叉树,判断该二叉树是否是平衡二叉树。
在这里,我们只需要考虑其平衡性,不需要考虑其是不是排序二叉树
平衡二叉树(Balanced Binary Tree),具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。
样例解释:
样例二叉树如图,为一颗平衡二叉树
注:我们约定空树是平衡二叉树。

数据范围:,树上节点的val值满足
要求:空间复杂度,时间复杂度

输入描述:
输入一棵二叉树的根节点


输出描述:
输出一个布尔类型的值
示例1

输入

{1,2,3,4,5,6,7}

输出

true
示例2

输入

{}

输出

true

说明:本题目包含复杂数据结构TreeNode,点此查看相关信息
int TreeHeight(struct TreeNode *pRoot) {
    int LeftHeight, RightHeight;
    if(pRoot==NULL)
        return 0;
    LeftHeight = TreeHeight(pRoot->left);
    RightHeight = TreeHeight(pRoot->right);
    return (LeftHeight>RightHeight ? LeftHeight: RightHeight)+1;
}
bool IsBalanced_Solution(struct TreeNode* pRoot ) {
    int HeightSUB;
    if(pRoot==NULL)
        return true;
    HeightSUB = TreeHeight(pRoot->left)-TreeHeight(pRoot->right);
    if(HeightSUB>1 || HeightSUB<-1)
        return false;
    if(IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right))
        return true;
    return false;
}

编辑于 2024-03-16 22:38:29 回复(0)

平衡二叉树是一种二叉树,它具有以下特点:

  1. 对于任意一个节点,它的左子树和右子树的高度差不超过1。
  2. 左子树和右子树也都是平衡二叉树。
    //求平衡二叉树的高度
    int maxDepth(struct TreeNode*root)
    {
        //根节点为空
        if(!root)
        {
            //高度为0
            return 0;
        }
        //遍历左右子树
        int lDepth=maxDepth(root->left);
        int rDepth=maxDepth(root->right);
        //返回较高的一边,算上根节点+1
        return lDepth>rDepth?(lDepth+1):(rDepth+1);
    }
    //判断是否为平衡二叉树
    bool IsBalanced_Solution(struct TreeNode* pRoot ) 
    {
        // write code here
        //根节点为空
        if(!pRoot)
        {
            return true;
        }
        //遍历左右子树高度
        int lDepth=maxDepth(pRoot->left);
        int rDepth=maxDepth(pRoot->right);
        //左右子树高度差
        int l_r_hight=abs(lDepth-rDepth);
        //左右子树高度差小于等于1,同时存在左右节点
        if(l_r_hight<=1&&IsBalanced_Solution(pRoot->left)&&IsBalanced_Solution(pRoot->right))
        {
            return true;
        }
        //不是平衡二叉树
        return false;
    }

发表于 2023-11-17 20:11:44 回复(0)
int bfs(struct TreeNode* root){
    if(root==NULL){
        return 0;
    }
    int left = bfs(root->left);
    int right = bfs(root->right);
    if(left==-1||right==-1){
        return -1;
    }
    if(((left - right)*(left-right))>1){
        return -1;
    }
    return (1+(left>right?left:right));
}

#include <stdbool.h>
bool IsBalanced_Solution(struct TreeNode* pRoot ) {
    int rtn = bfs(pRoot);
    return rtn!=-1;
}

发表于 2023-09-23 19:16:57 回复(0)
判断所有节点的左子树高度和右子树高度的差值,差值超过1,就结束返回false,不超过1,就一直判断,直到判断到结点为空时,就证明比遍历完都没有发现两个子树高度差超过1的情况,就可以断定该二叉树是平衡二叉树
判断每个结点时,调用getdeep函数,可以知道该节点左子树和右子树的高度,利用两个高度进行判断,就可以得出其两个子树的高度差。

int getdeep(struct TreeNode* root)
{
    if(root==NULL)
    {
        return 0;
    }

    int left=getdeep(root->left)+1;
    int right=getdeep(root->right)+1;

    return left>right?left:right;
}

bool IsBalanced_Solution(struct TreeNode* pRoot ) {
    // write code here
    if(pRoot==NULL)
    {
        return true;
    }
    int lift=getdeep(pRoot->left);
    int right=getdeep(pRoot->right);
    if(lift-right>1||lift-right<-1)
    {
        return false;
    }
    return IsBalanced_Solution(pRoot->left)&&IsBalanced_Solution(pRoot->right);
}
发表于 2023-08-07 20:04:26 回复(0)
int height_tree(struct TreeNode* pRoot)
{
    if(NULL==pRoot)return 0;
    int lh=height_tree(pRoot->left);
    int rh=height_tree(pRoot->right);
    return lh>rh?lh+1:rh+1;
}
bool IsBalanced_Solution(struct TreeNode* pRoot ) {
     if (NULL==pRoot) return true;
     int leftHeight = height_tree(pRoot->left);
     int rightHeight = height_tree(pRoot->right);
    return abs(leftHeight - rightHeight) <= 1 &&
        IsBalanced_Solution(pRoot->left) &&
          IsBalanced_Solution(pRoot->right) ;
}

发表于 2023-07-20 09:37:06 回复(0)
 int CalDepth(struct TreeNode* pRoot)
 {
    if(!pRoot)
    {
        return 0;
    }
    int left=CalDepth(pRoot->left);
    int right=0;
    if(-1!=left)
    {
        right=CalDepth(pRoot->right);
    }
    if(-1!=left&&-1!=right&&-1<=left-right&&left-right<=1)
    {
        if(left>right)
        {
            return left+1;
        }
        else return right+1;
    }
    return -1;
 }

bool IsBalanced_Solution(struct TreeNode* pRoot )
{
    // write code here
    if(!pRoot)
    {
        return true;
    }
    int flag=CalDepth(pRoot);
    if(-1==flag)
    {
        return false;
    }
    else
    {
        return true;
    }
}
发表于 2023-07-10 10:02:21 回复(0)

调试了半天没调出来,思路是对的,最后还是照抄大佬的答案吧

int count(struct TreeNode* pRoot) {
    if(pRoot != NULL) {
        int left = count(pRoot->left);
        int right = count(pRoot->right);
        return (left>right?left:right)+1;
    }
    return 0;
}
bool IsBalanced_Solution(struct TreeNode* pRoot ) {
    // write code here
    if (pRoot == NULL) return true;
    int left=0,right=0,goal=false;
    left=count(pRoot->left);
    right=count(pRoot->right);
    int ret = left - right;
    if (ret <= 1 && ret >= -1) goal=true;
    return goal&&IsBalanced_Solution(pRoot->left)&&IsBalanced_Solution(pRoot->right);
}
发表于 2022-11-11 14:28:57 回复(0)
int GetDeep(struct TreeNode *root){
    if(root != NULL){
        int left = GetDeep(root->left);
        int right = GetDeep(root->right);
        return (left>right?left:right)+1;
    }
    return 0;
}

int IsBalanced_Solution(struct TreeNode* pRoot ) {
    if(pRoot == NULL) return 1;
    int left = GetDeep(pRoot->left);
    int right = GetDeep(pRoot->right);
    if(abs(left - right) > 1) return 0;
    else return IsBalanced_Solution(pRoot->left)&&IsBalanced_Solution(pRoot->right);
}

发表于 2022-03-17 19:47:36 回复(0)
/**
 * struct TreeNode {
 * int val;
 * struct TreeNode *left;
 * struct TreeNode *right;
 * };
 */
/**
 *
 * @param pRoot TreeNode类
 * @return bool布尔型
 */
int height(struct TreeNode* ppRoot)
{
    if(ppRoot == NULL) return 0;
   
    int left_height = 0 , right_height = 0;
    left_height = height(ppRoot->left);
    right_height = height(ppRoot->right);
    if(left_height > right_height) return 1+left_height;
    else return 1+right_height;
}
bool IsBalanced_Solution(struct TreeNode* pRoot ) {
    // write code here
    if(pRoot == NULL) return true;
   
    if(abs(height(pRoot->left) - height((pRoot->right))) > 1) return false;
    bool b1=true,b2=true;
    b1=IsBalanced_Solution(pRoot->left);
    b2=IsBalanced_Solution(pRoot->right);
    if(b1 && b2) return true;
    else return false;
   
   
   
}
发表于 2021-08-20 15:17:08 回复(0)
#include <stdbool.h>

int dfs(struct TreeNode* root, bool* ans) {
  if (!root) return 0;
  const int lh = dfs(root->left,  ans); // lh == left subtree height
  const int rh = dfs(root->right, ans); // rh == right subtree height
  if (abs(lh - rh) > 1) *ans = false;
  return 1 + fmax(lh, rh);
}

bool IsBalanced_Solution(struct TreeNode* pRoot ) {
  if (!pRoot) return true;
  bool ans = true;
  dfs(pRoot, &ans);
  return ans;
}

发表于 2021-07-29 08:13:58 回复(0)

问题信息

难度:
10条回答 112189浏览

热门推荐

通过挑战的用户

查看代码