本问题对应的 leetcode 原文链接:剑指 Offer 55 – II. 平衡二叉树

问题描述

输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过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

限制:

  • 0 <= 树的结点个数 <= 10000

解题思路

视频讲解直达: 本题视频讲解

代码实现

class Solution {
    // nlogn 空间 On   方法2:时间 On,空间O
    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;
        if(maxDepth(root) == -1) return false;
        return true;

    }

    public int maxDepth(TreeNode root) {
        if(root == null) return 0;
        int left = maxDepth(root.left);
        if(left == -1) return -1;
        int right = maxDepth(root.right);
        if(right == -1) return -1;
        // 返回-1表示不符合条件了
        if(Math.abs(left - right) > 1) return -1;

        return Math.max(left, right) + 1;
    }
}

Python

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def isBalanced(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if root is None:
            return True
        if self.maxDepth(root) == -1:
            return False
        return True

    def maxDepth(self, root):
        if root is None:
            return 0
        left = self.maxDepth(root.left)
        if left == -1:
            return -1
        right = self.maxDepth(root.right)
        if right == -1:
            return -1
        # 返回-1表示不符合条件了
        if abs(left - right) > 1:
            return -1
        return max(left, right) + 1

C++

class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if (root == nullptr) return true;
        if (maxDepth(root) == -1) return false;
        return true;
    }

    int maxDepth(TreeNode* root) {
        if (root == nullptr) return 0;
        int left = maxDepth(root->left);
        if (left == -1) return -1;
        int right = maxDepth(root->right);
        if (right == -1) return -1;

        if (abs(left - right) > 1) return -1;

        return max(left, right) + 1;
    }
};

Go

import "math"

func isBalanced(root *TreeNode) bool {
    if root == nil {
        return true
    }
    if maxDepth(root) == -1 {
        return false
    }
    return true
}

func maxDepth(root *TreeNode) int {
    if root == nil {
        return 0
    }
    left := maxDepth(root.Left)
    if left == -1 {
        return -1
    }
    right := maxDepth(root.Right)
    if right == -1 {
        return -1
    }
    if math.Abs(float64(left-right)) > 1 {
        return -1
    }
    return int(math.Max(float64(left), float64(right))) + 1
}

JS

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isBalanced = function(root) {
    if (root == null) return true;
    if (maxDepth(root) == -1) return false;
    return true;

};

function maxDepth(root) {
    if (root == null) return 0;
    let left = maxDepth(root.left);
    if (left == -1) return -1;
    let right = maxDepth(root.right);
    if (right == -1) return -1;
    // 返回-1表示不符合条件了
    if (Math.abs(left - right) > 1) return -1;

    return Math.max(left, right) + 1;
}

发表回复

后才能评论

评论(1)

  • 优秀市民 普通 2022年10月28日 上午12:08
    class Solution {
        public boolean isBalanced(TreeNode root) {
            if(root == null) return true;
            if(maxDepth(root) == -1) return false;
            return true;
    
    
        }
    
        public int maxDepth(TreeNode root){
            if(root == null) return  0;
            int l = maxDepth(root.left);
            if(l == -1) return -1;
            int r = maxDepth(root.right);
            if(r == -1) return -1;
    
            //返回-1说明不符合条件
            if(Math.abs(l - r) > 1) return -1;
            return Math.max(l,r) + 1;
        }
    }
    

    on,on