leetcode 110. balanced-binary-tree 平衡二叉树 python3

时间:2020-10-12

题目地址:https://leetcode-cn.com/problems/balanced-binary-tree/

题目难度:Easy

题目描述:

给定一个二叉树,判断它是否是高度平衡的二叉树。

本题中,一棵高度平衡二叉树定义为:

一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。

示例 1:

给定二叉树 [3,9,20,null,null,15,7]

    3
   / \
  9  20
    /  \
   15   7
返回 true 。

示例 2:

给定二叉树 [1,2,2,3,3,null,null,4,4]

       1
      / \
     2   2
    / \
   3   3
  / \
 4   4
返回 false 。


思路1:从顶至底(暴力法)

借用104的https://blog.csdn.net/isabloomingtree/article/details/106135722

构造一个获取当前节点最大深度的方法 depth(root) ,通过比较此子树的左右子树的最大高度差abs(depth(root.left) - depth(root.right)),来判断此子树是否是二叉平衡树。若树的所有子树都平衡时,此树才平衡。

一开始没有递归没有子树,只遍历了根节点的左右子树

代码段1:通过

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if root == None:
            return 0
        else:
            left_depth = self.maxDepth(root.left)
            right_depth = self.maxDepth(root.right)
            return max(left_depth, right_depth) + 1

    def isBalanced(self, root: TreeNode) -> bool:
        if not root:
            return True
        if root:
            left_depth = self.maxDepth(root.left)
            right_depth = self.maxDepth(root.right)
            #print(left_depth,right_depth)
            if abs((left_depth + 1) - (right_depth + 1 )) > 1:
                return False
            #print(self.isBalanced(root.left),self.isBalanced(root.right))
            return self.isBalanced(root.left) and self.isBalanced(root.right)

总结:

  1. 自己写的有点乱且繁琐,看下大佬优化的
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if root == None:
            return 0
        return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1

    def isBalanced(self, root: TreeNode) -> bool:
        if not root:
            return True
        return abs(self.maxDepth(root.left) - self.maxDepth(root.right)) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right)

思路2:从底至顶(提前阻断)
此方法为本题的最优解法,但“从底至顶”的思路不易第一时间想到。

思路是对二叉树做先序遍历,从底至顶返回子树最大高度,若判定某子树不是平衡树则 “剪枝” ,直接向上返回。

算法流程:
recur(root):

递归返回值:
当节点root 左 / 右子树的高度差 < 2<2 :则返回以节点root为根节点的子树的最大高度,即节点 root 的左右子树中最大高度加 11 ( max(left, right) + 1 );
当节点root 左 / 右子树的高度差 \geq 2≥2 :则返回 -1−1 ,代表 此子树不是平衡树 。
递归终止条件:
当越过叶子节点时,返回高度 00 ;
当左(右)子树高度 left== -1 时,代表此子树的 左(右)子树 不是平衡树,因此直接返回 -1−1 ;
isBalanced(root) :

返回值: 若 recur(root) != 1 ,则说明此树平衡,返回 truetrue ; 否则返回 falsefalse 。
复杂度分析:
时间复杂度 O(N)O(N): NN 为树的节点数;最差情况下,需要递归遍历树的所有节点。
空间复杂度 O(N)O(N): 最差情况下(树退化为链表时),系统递归需要使用 O(N)O(N) 的栈空间。

代码段2:通过

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isBalanced(self, root: TreeNode) -> int:
        return self.recur(root) != -1

    def recur(self, root: TreeNode) -> bool:
        if not root:
            return 0
        left = self.recur(root.left)
        if left == -1: return -1
        right = self.recur(root.right)
        if right == -1: return -1
        return max(left, right) + 1 if abs(left - right) < 2 else -1

总结:

  1. 这个简直厉害了

你可能感兴趣的:(python,leetcode,leetcode,python)