资讯专栏INFORMATION COLUMN

LeetCode 110 Balanced Binary Tree 平衡二叉树

anquan / 1984人阅读

摘要:题意判断一颗二叉树是否是平衡二叉树,平衡二叉树的定义为,每个节点的左右子树深度相差小于这是和求最大深度的结合在一起,可以考虑写个函数找到拿到左右子树的深度,然后递归调用函数判断左右子树是否也是平衡的,得到最终的结果。

LeetCode 110 Balanced Binary Tree

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.

Example 1:

Given the following tree [3,9,20,null,null,15,7]:

    3
   / 
  9  20
    /  
   15   7

Return true.

Example 2:

Given the following tree [1,2,2,3,3,null,null,4,4]:

       1
      / 
     2   2
    / 
   3   3
  / 
 4   4

Return false.

Solution 1:
这是和求最大深度的结合在一起,可以考虑写个helper函数找到拿到左右子树的深度,然后递归调用isBalanced函数判断左右子树是否也是平衡的,得到最终的结果。时间复杂度O(n^2)

    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        int leftDep = depthHelper(root.left);
        int rightDep = depthHelper(root.right);
        if (Math.abs(leftDep - rightDep) <= 1 && isBalanced(root.left) && isBalanced(root.right)) {
            return true;
        }
        return false;
    }
    private int depthHelper(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return Math.max(depthHelper(root.left), depthHelper(root.right)) + 1;
    }

Solution 2:
解题思路:
再来看个O(n)的递归解法,相比上面的方法要更巧妙。二叉树的深度如果左右相差大于1,则我们在递归的helper函数中直接return -1,那么我们在递归的过程中得到左子树的深度,如果=-1,就说明二叉树不平衡,也得到右子树的深度,如果=-1,也说明不平衡,如果左右子树之差大于1,返回-1,如果都是valid,则每层都以最深的子树深度+1返回深度。

    public boolean isBalanced(TreeNode root) {
        return dfsHeight(root) != -1;
    }
    //helper function, get the height
    public int dfsHeight(TreeNode root){
        if (root == null) {
            return 0;
        }
        int leftHeight = dfsHeight(root.left);
        if (leftHeight == -1) {
            return -1;
        }
        int rightHeight = dfsHeight(root.right);
        if (rightHeight == -1) {
            return -1;
        }
        if (Math.abs(leftHeight - rightHeight) > 1) {
            return -1;
        }
        return Math.max(leftHeight, rightHeight) + 1;
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/71795.html

相关文章

  • [Leetcode] Balanced Binary Tree 平衡叉树

    摘要:在递归中,从叶子结点开始一层层返回高度,叶子结点是。我们返回代表非平衡,返回自然数代表有效的子树高度。 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a...

    lylwyy2016 评论0 收藏0
  • [LintCode/LeetCode] Balanced Binary Tree

    摘要:根据二叉平衡树的定义,我们先写一个求二叉树最大深度的函数。在主函数中,利用比较左右子树的差值来判断当前结点的平衡性,如果不满足则返回。 Problem Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as...

    morgan 评论0 收藏0
  • LeetCode 攻略 - 2019 年 7 月上半月汇总(55 题攻略)

    摘要:微信公众号记录截图记录截图目前关于这块算法与数据结构的安排前。已攻略返回目录目前已攻略篇文章。会根据题解以及留言内容,进行补充,并添加上提供题解的小伙伴的昵称和地址。本许可协议授权之外的使用权限可以从处获得。 Create by jsliang on 2019-07-15 11:54:45 Recently revised in 2019-07-15 15:25:25 一 目录 不...

    warmcheng 评论0 收藏0
  • 前端 | 每天一个 LeetCode

    摘要:在线网站地址我的微信公众号完整题目列表从年月日起,每天更新一题,顺序从易到难,目前已更新个题。这是项目地址欢迎一起交流学习。 这篇文章记录我练习的 LeetCode 题目,语言 JavaScript。 在线网站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公众号: showImg(htt...

    张汉庆 评论0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月汇总(100 题攻略)

    摘要:月下半旬攻略道题,目前已攻略题。目前简单难度攻略已经到题,所以后面会调整自己,在刷算法与数据结构的同时,攻略中等难度的题目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道题,目前已攻略 100 题。 一 目录 不折腾的前端,和咸鱼有什么区别...

    tain335 评论0 收藏0

发表评论

0条评论

anquan

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<