【二叉树专题】剑指 Offer 55 – II. 平衡二叉树

本问题对应课程链接:剑指offer刷题第四期

要求:

1、请打卡你的解题代码,如果能简单说一下思路则简单说一下思路。
2、给出你代码所用的时间复杂度和空间复杂度

共以下 38 个回答

  • 桎梏 普通 2023年8月11日 下午9:04
    class Solution {
    
        // 1.确定递归函数参数和返回值:此处递归函数实际为求当前节点的高度,如果其左右子树高度相差大于1 返回-1 否则返回其高度
        // 2.确定终止条件:如果根节点为空 则其高度为0
        // 3.确定递归体:求左右子树的高度 若其值为-1 则返回-1
        public boolean isBalanced(TreeNode root) {
            return getHeight(root) != -1;
        }
    
        public int getHeight(TreeNode root) {
            if(root == null) return 0;
    
            int leftHeight = getHeight(root.left);
            if(leftHeight == -1) return -1;
            int rightHeight = getHeight(root.right);
            if(rightHeight == -1) return -1;
            if(Math.abs(leftHeight - rightHeight) > 1) {
                return -1;
            }
    
            return Math.max(leftHeight, rightHeight) + 1;
        }
    }
    
    0 赞同 0 条回复
  • hi 普通 2023年8月4日 下午11:11
    1. 与前一题求二叉树最大深度有类似之处,都需要递归求二叉树左、右子树的深度
    2. 只是这一题需在求左右子树的时候,及时判断左、右子树的深度是否超过1
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        bool ans = true; //全局变量
        bool isBalanced(TreeNode* root) {
            maxDepth(root);
            return ans;
        }
    
        int maxDepth(TreeNode* root)
        {
            if(!root) return 0;
            int left = maxDepth(root -> left);
            int right = maxDepth(root -> right);
            if(abs(left - right) > 1) ans = false;
            return max(left, right) + 1;
        }
    };
    
    0 赞同 0 条回复
  • 今天没时间刷题 普通 2023年7月30日 下午3:37
    时间On,空间On
    class Solution {
    public:
        bool isBalanced(TreeNode* root) {
            if(!root)
            {
                return true;
            }
    
            return depth(root) != -1;
    
        }
    
        int depth(TreeNode* root)
        {
            if(!root)
            {
                return 0;
            }
    
            int left = depth(root->left);
            if(left == -1)
            {
                return -1;
            }
            int right = depth(root->right);
            if(right == -1)
            {
                return -1;
            }
    
            if (abs(left - right) > 1)
            {
                return -1;
            }
            return max(left, right) + 1;
        }
    };
    
    
    0 赞同 0 条回复
  • wowo 普通 2023年7月26日 上午12:07

    递归:首先判断左右子树的高度相差不超过1,然后再判断左右子树是否为平衡二叉树
    高度函数也是基于递归
    时间复杂度O(n2)
    空间复杂度O(n)

    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        def isBalanced(self, root: TreeNode) -> bool:
            if not root:
                return True
            depth_left = self.height(root.left)
            depth_right = self.height(root.right)
            if abs(depth_left-depth_right) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right):
                return True
            return False
    
        def height(self, root:TreeNode) -> int:
            if not root:
                return 0
            return max(self.height(root.left), self.height(root.right))+1
    
    0 赞同 0 条回复
  • finalrgy 普通 2023年7月22日 下午4:03
    func height(root *TreeNode) int {
        if root == nil {
            return 0
        }
        left := height(root.Left)
        right := height(root.Right)
        if left < right {
            left = right
        }
        return left + 1
    }
    
    func isBalanced(root *TreeNode) bool {
        if root == nil {
            return true
        }
        if isBalanced(root.Left) && isBalanced(root.Right) {
            diff := height(root.Left) - height(root.Right)
            if diff < 0 {
                diff *= -1
            }
            if diff <= 1 {
                return true
            }
        }
    
        return false
    }
    

    递归比较每个结点左子树和右子树高度差

    0 赞同 0 条回复
  • doordie. 普通 2023年7月18日 下午2:00
    class Solution {
         public boolean isBalanced(TreeNode root) {
            if (root == null){
                return true;
            }
            return Math.abs(depth(root.left)-depth(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
    
        }
        private int depth(TreeNode root) {
            if (root == null) return 0;
            return Math.max(depth(root.left), depth(root.right)) + 1;
        }
    }
    

    时间复杂度O(n*logN)
    空间复杂度O(n)
    maxDepth:后序遍历
    idBalanced:先序遍历

    0 赞同 0 条回复
  • 朱志刚 普通 2023年7月17日 下午7:08
    class Solution {
    public:
        bool isBalanced(TreeNode* root) {
    
            if(maxDepth(root)==-1)return false;
            return true;
    
        }
        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;
    
            if(abs(left-right)>1)return -1;
    
            return max(left,right)+1;
        }
    
    };
    

    通过

    0 赞同 0 条回复
  • 枫梓。 普通 2023年7月17日 下午12:39
    int height(TreeNode* root) {
            if(root == NULL) return 0;
            if(height(root->left) == -1 || height(root->right) == -1 || abs(height(root->left) - height(root->right)) > 1 ) {
                return -1;
            } else {
                return max(height(root->left), height(root->right)) + 1;
            }
        }
        bool isBalanced(TreeNode* root) {
            return height(root) >=0;
        }
    

    从叶子结点开始,判断是否是平衡的,逐渐往上判断直至根节点。 时间复杂度,对每个结点都有判断故为O(n) 空间复杂度,递归栈为O(n)

    0 赞同 0 条回复
  • 今天你刷题了嘛 普通 2023年7月16日 下午10:18
    class Solution {
        public boolean isBalanced(TreeNode root) {
            // 时间复杂度是O(n),空间复杂度是O(n)
            if(root==null) return true;
            if(depth(root)==-1) return false;
            return true;
        }
    
        public int depth(TreeNode root){
            // 在计算深度时,加上判断,可以避免重复计算
            if(root==null) return 0;
            int left = depth(root.left);
            if(left==-1) return -1;
            int right = depth(root.right);
            if(right==-1) return -1;
            if(Math.abs(left-right)>1) return -1;
            return Math.max(left+1,right+1);
        }
    }
    
    0 赞同 0 条回复
  • 神经蛙 普通 2023年7月16日 下午12:37
     /**
        时间复杂度O(n*logN)
        空间复杂度O(n)
        maxDepth:后序遍历
        idBalanced:先序遍历
    */
    class Solution {
        public boolean isBalanced(TreeNode root) {
            if (root == null) {
                return true;
            }
            return Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
        }
    
        public int maxDepth(TreeNode node) {
            if (node == null) {
                return 0;
            }
    
            int leftLength = maxDepth(node.left);
            int rightLength = maxDepth(node.right);
    
            return Math.max(leftLength, rightLength) + 1;
        }
    }
    
    0 赞同 0 条回复
  • 周小二 普通 2023年7月15日 下午11:48
    class Solution {
        //使用flag做剪枝,如果已有不平衡子树,则不继续往下递归
        private boolean flag = true;
        public boolean isBalanced(TreeNode root) {
            height(root);
            return flag;
        }
    
        public int height(TreeNode root) {
            if (root == null || !flag) {
                return 0;
            }
            int left = (height(root.left));
            int right = (height(root.right));
            if (Math.abs(left - right) > 1) {
                flag = false;
            }
            return Math.max(left, right) + 1;
        }
    }
    
    0 赞同 0 条回复
  • 片言莫语 普通 2023年7月15日 下午9:10
    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;
        }
    }
    
    0 赞同 0 条回复
  • ppopp 普通 2023年7月15日 下午8:32

    /**中文
    * Definition for a binary tree node.
    * struct TreeNode {
    * int val;
    * TreeNode left;
    * TreeNode *right;
    * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    * };
    */
    class Solution {
    public:
    bool isBalanced(TreeNode
    root) {
    if(root nullptr) return true;
    int res = abs(Depth(root->right) – Depth(root -> left));
    return res <= 1 && isBalanced(root->left) && isBalanced(root->right);
    }

    int Depth(TreeNode* root){
        if(root == nullptr) return 0;
        return max(Depth(root->left), Depth(root->right)) + 1;
    }
    

    };

    0 赞同 0 条回复
  • Marshan 普通 2023年7月15日 下午7:45
    //在求深度的基础上稍做修改即可
    class Solution {
    public:
        bool flag = true;
        bool isBalanced(TreeNode* root) {
            if(root == nullptr){
                return true;
            }
            // int leftDepth = helper(root->left);
            // int rightDepth = helper(root->right);
            // if(abs(leftDepth-rightDepth) > 1){
            //     return false;
            // }
            int depth = helper(root);
            return flag;
        }
        int helper(TreeNode *root){
            if(root == nullptr){
                return 0;
            }
    
            int leftDepth = helper(root->left);
            int rightDepth = helper(root->right);
    
            if(abs(leftDepth-rightDepth) > 1){
                flag = false;
            }
            return max(leftDepth,rightDepth) + 1;
        }
    };
    
    0 赞同 0 条回复
  • 人生何处不青山 普通 2023年7月15日 下午6:07

    上一题的简单应用

    /**
     * Definition for a binary tree node.
     * class TreeNode {
     *     val: number
     *     left: TreeNode | null
     *     right: TreeNode | null
     *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
     *         this.val = (val===undefined ? 0 : val)
     *         this.left = (left===undefined ? null : left)
     *         this.right = (right===undefined ? null : right)
     *     }
     * }
     */
    
    function isBalanced(root: TreeNode | null): boolean {
        let result = true;
        const dfs = (node: TreeNode | null): number => {
            if(!node) return 0;
            let left = dfs(node.left);
            let right = dfs(node.right);
            if(Math.abs(left - right) > 1) result = false;
            return 1 + Math.max(left,right);
        }
        dfs(root);
        return result;
    };
    
    0 赞同 0 条回复
  • June 普通 2023年7月15日 下午3:52
    /**
     * Definition for a binary tree node.
     * type TreeNode struct {
     *     Val int
     *     Left *TreeNode
     *     Right *TreeNode
     * }
     */
    import "math"
    func isBalanced(root *TreeNode) bool {
        return Height(root) >= 0
    }
    
    func Height(root *TreeNode) int {
        if root == nil{
            return 0
        }
        var lh int = Height(root.Left)
        var rh int = Height(root.Right)
        if lh == -1 || rh == -1 || math.Abs(float64(lh - rh)) > 1 {
            return -1
        }
    
        return int(math.Max(float64(lh),float64(rh))) + 1
    }
    // 时间复杂度 O(n) 空间复杂度 O(n)
    
    0 赞同 0 条回复
  • Terence💧 普通 2023年7月15日 下午1:36
    /**
     * Definition for a binary tree node.
     * type TreeNode struct {
     *     Val int
     *     Left *TreeNode
     *     Right *TreeNode
     * }
     */
    func isBalanced(root *TreeNode) bool {
        if root == nil {
            return true
        }
        return abs(height(root.Left) - height(root.Right)) <= 1 && isBalanced(root.Left) && isBalanced(root.Right)//子树也需要考虑平衡
    }
    
    func height(root *TreeNode) int{
        if root == nil {
            return 0
        }
        return max(height(root.Left), height(root.Right)) + 1
    }
    
    
    func max(a,b int) int {
        if a > b {
            return a
        }
        return b
    }
    
    func abs(a int) int {
        if a < 0 {
            return a * -1
        }
        return a
    }
    
    0 赞同 0 条回复
  • Mby 普通 2023年7月14日 下午10:02
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        //记录已经返回的值进行优化
        //优化后的时间复杂度:On 空间复杂度: On
        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;
        }
    }
    
    0 赞同 0 条回复
  • #00456B 普通 2023年7月14日 下午6:31

    自底向上递归

    class Solution {
    public:
        bool isBalanced(TreeNode* root) {
            return maxDepth(root) != -1;
        }
        int maxDepth(TreeNode* root){
            if(root == NULL){
                return 0;
            }
            int leftDepth = maxDepth(root->left);
            int rightDepth = maxDepth(root->right);
            if(abs(leftDepth - rightDepth) > 1){
                return -1;
            }
            if(leftDepth == -1 || rightDepth == -1){
                return -1;
            }
            return max(leftDepth,rightDepth) + 1;
        }
    };
    
    0 赞同 0 条回复
  • CKX 普通 2023年7月14日 下午5:06
    /**
     * Definition for a binary tree node.
     * type TreeNode struct {
     *     Val int
     *     Left *TreeNode
     *     Right *TreeNode
     * }
     */
    func isBalanced(root *TreeNode) bool {
        if root == nil {
            return true
        }
    
        return abs(maxDepth(root.Left), maxDepth(root.Right)) <= 1 && isBalanced(root.Left) && isBalanced(root.Right)
    
    }
    
    func maxDepth(root *TreeNode) int {
        if root == nil {
            return 0
        }
    
        left := maxDepth(root.Left)
        right := maxDepth(root.Right)
    
        return max(left, right) + 1
    
    }
    
    func max(a , b int) int {
        if a > b {
            return a 
        }
        return b
    }
    
    func abs(a, b int) int {
        if (a - b) < 0 {
            return b - a
        }
    
        return a - b
    }
    
    • 时间O(n*n)
    • 空间O(n)
    0 赞同 0 条回复
  • Fighting boy😀 普通 2023年7月14日 下午4:26

    // 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;
    }
    

    };

    0 赞同 0 条回复
  • CaCO3 永久会员 2023年7月13日 下午8:25

    ”’java
    class Solution {
    public boolean isBalanced(TreeNode root) {
    if(root null){
    return true;
    }
    if(Math.abs(maxDepth(root.left)-maxDepth(root.right)) <= 1){
    return isBalanced(root.left) && isBalanced(root.right);
    }
    return false;
    }
    public int maxDepth(TreeNode root){
    if(root
    null){
    return 0;
    }
    int leftDepth = maxDepth(root.left);
    int rightDepth = maxDepth(root.right);
    return leftDepth > rightDepth ? leftDepth+1:rightDepth+1;
    }
    }
    ”’
    时间复杂度O(n)
    空间复杂度O(n)

    0 赞同 0 条回复
  • 北辰 普通 2023年7月13日 下午7:34
    //自底向上递归
        public static boolean isBalanced(TreeNode root) {
            return treeHeight(root)>=0;
        }
    
        public static int treeHeight(TreeNode root) {
            if(root==null) return 0;
            int leftHeight = treeHeight(root.left);
            int rightHeight = treeHeight(root.right);
            if(leftHeight==-1||rightHeight==-1||Math.abs(leftHeight-rightHeight)>1) {
                return -1;
            }else {
                return Math.max(leftHeight, rightHeight)+1;
            }
        }
    

    时间复杂度:O(n)
    空间复杂度:O(n)

    0 赞同 0 条回复
  • 坏掉的世界 永久会员 2023年7月13日 下午7:13

    代码

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        bool isBalance = true;
        bool isBalanced(TreeNode* root) {
            if(!root)
                return true;
            dfs(root);
            return isBalance;
        }
        int dfs(TreeNode* root){
            if(!root)
                return 0;
            int left = dfs(root -> left);
            int right = dfs(root -> right);
            if(abs(left - right)>1){
                isBalance = false;
            }
            return max(left,right)+1;
        }
    };
    
    0 赞同 0 条回复
  • NullPointerException 普通 2023年7月13日 下午6:58

    boolean jude =true;
    public boolean isBalanced(TreeNode root) {
    //左右子树最大深度不能超过1
    isBalanced1(root);
    return jude;
    }
    public int isBalanced1(TreeNode root) {
    if(rootnull) return 0;

        int left =isBalanced1(root.left);
        int right =isBalanced1(root.right);
        if(Math.abs(left-right)>1)
        {
            jude =false;
        }
        return Math.max(left,right)+1;
    
    
    }
    

    }

    0 赞同 0 条回复
  • V2ero 普通 2023年7月13日 下午4:13

    递归调用

    function isBalanced(root: TreeNode | null): boolean {
        if (!root) {
            return true;
        }
        return (
            Math.abs(dfsHeight(root.left) - dfsHeight(root.right)) <= 1 &&
            isBalanced(root.left) &&
            isBalanced(root.right)
        );
    }
    
    const dfsHeight = (root: TreeNode | null) => {
        if (!root) {
            return 0;
        } else {
            const leftHeight = dfsHeight(root.left);
            const rightHeight = dfsHeight(root.right);
            return Math.max(leftHeight, rightHeight) + 1;
        }
    };
    
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月13日 下午3:50
    // 练习
    public boolean isBalanced(TreeNode root) {
            if (root==null)
                return true;
            if (Math.abs(dfs(root.left)-dfs(root.right))>1)
                return false;
            return isBalanced(root.left)&&isBalanced(root.right);
        }
    
        private int dfs(TreeNode root) {
            if (root==null)
                return 0;
            return Math.max(dfs(root.left), dfs(root.right))+1;
        }
    
    0 赞同 0 条回复
  • momo 普通 2023年7月13日 下午3:27

    时间复杂度O(nlogn)
    空间复杂度O(n)

    class Solution {
        public boolean isBalanced(TreeNode root) {
            if (root == null) return true;
            return Math.abs(maxDepth(root.left) - maxDepth(root.right)) < 2 
            && isBalanced(root.left) 
            && isBalanced(root.right);
        }
        private int maxDepth(TreeNode root) {
            if (root == null) return 0;
            return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
        }
    }
    
    0 赞同 0 条回复
  •  普通 2023年7月13日 下午3:11

    c++ 代码 递归调用

    
    class Solution {
        bool istrue=true;
    public:
        bool isBalanced(TreeNode* root)
         {
                getheight(root);
                 return istrue;
    
     }  
          int getheight(TreeNode* root)
            {
                 if(!root)
                    return false;
                 int  Tleft=getheight(root->left);
                  int Tright=getheight(root->right);
               if(Tleft-Tright>1||Tright-Tleft>1)
                    istrue=false;
                return Tleft>Tright?Tleft+1:Tright+1;
            } 
    

    };

    0 赞同 0 条回复
  • 阿尚 普通 2023年7月13日 上午9:29
    //本质还是求树的高度
    class Solution {
        public boolean isBalanced(TreeNode root) {
            return process(root) == -1 ? false : true;
        }
    
        public int process(TreeNode root){
            if(root == null) return 0;
    
            int left = process(root.left);
            if(left == -1) return left;
            int right = process(root.right);
            if(right == -1) return right;
    
            if(Math.abs(left - right) > 1) return -1;
    
            return Math.max(left, right) + 1;
        }
    }
    
    0 赞同 0 条回复
  • 波雅汉库克 普通 2023年7月13日 上午5:57
    class Solution {
    public:
        int height(TreeNode* root){
            if(root == nullptr){
                return 0;
            }
            int left = height(root->left);
            int right = height(root->right);
            if(left == -1 || right == -1 || abs(left-right)>1){
                return -1;
            } 
            return max(left,right)+1;
        }
        bool isBalanced(TreeNode* root) {
            return height(root)!=-1;
        }
    };
    

    时间: O(n)
    空间:O(n)

    0 赞同 0 条回复
  • 长河呼叫黄江 普通 2023年7月13日 上午12:08

    Day6:下面是代码

    class Solution {
        public boolean isBalanced(TreeNode root) {
            if(root == null) return true;
            return Math.abs(depth(root.left) - depth(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
    
        }
    
        private int depth(TreeNode root){
            if(root == null) return 0;
            return Math.max(depth(root.left), depth(root.right)) + 1;
    
        }
    }
    
    0 赞同 0 条回复
  • 艺 普通 2023年7月12日 下午10:58

    “`c++
    //时间复杂度:O(n)
    //空间复杂度:O(n)
    class Solution {
    public:
    bool isBalanced(TreeNode* root) {
    if(root==NULL) return true;
    if(maxDepth(root)==-1) return false;
    return true;
    }
    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;
    if(abs(l-r)>1) return -1;
    return max(l,r)+1;
    }
    };

    0 赞同 0 条回复
  • 土豆芋泥包_ 普通 2023年7月12日 下午8:20

    代码

    class Solution {
        public boolean isBalanced(TreeNode root) {
            if(root==null){
                return true;
            }
            int left = maxDepth(root.left);
            int right = maxDepth(root.right);
    
            return Math.abs(right-left)<=1 && isBalanced(root.left) && isBalanced(root.right);
    
        }
    
        int maxDepth(TreeNode root){
            if(root==null) return 0;
            int left = maxDepth(root.left);
            int right = maxDepth(root.right);
            return Math.max(left,right)+1;
        }
    
    }
    
    0 赞同 0 条回复
  • ONE PIECE 普通 2023年7月12日 下午6:05
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public boolean isBalanced(TreeNode root) {
            if(root == null) return true;
            if(Math.abs(len(root.left)-len(root.right))>1) return false;
            return isBalanced(root.left)&&isBalanced(root.right);
        }
        public int len(TreeNode node){
            if(node==null) return 0;
            int left = len(node.left);
            int right = len(node.right);
            return 1+Math.max(left,right);
        }
    }
    

    时间复杂度:?

    0 赞同 0 条回复
  • Luminous 普通 2023年7月12日 下午6:03
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public boolean isBalanced(TreeNode root) {
            if(root==null) return true;
            if(depth(root)!=-1) return true;
            return false;
        }
    
        public int depth(TreeNode root){
            if(root==null){
                return 0;
            }
            int left=depth(root.left);
            if(left==-1) return -1;
            int right=depth(root.right);
            if(right==-1) return -1;
            if(Math.abs(left-right)>1) return -1;
            return Math.max(left,right)+1;
        }
    }
    

    时间复杂度:O(n)
    空间复杂度:O(n)

    0 赞同 0 条回复
  • AD钙奶 普通 2023年7月12日 下午4:29

    打卡

    class Solution {
        public boolean isBalanced(TreeNode root) {
            return getHeight(root) != -1 ;
        }
    
        public int getHeight(TreeNode root) {
            if(root == null) return 0;
            int leftHeight = getHeight(root.left);
            if(leftHeight == -1) return -1;
            int rightHeight = getHeight(root.right);
            if(rightHeight == -1) return -1;
    
            return Math.abs(leftHeight - rightHeight) > 1 ? -1 : 1 + Math.max(leftHeight, rightHeight);
        }
    }
    
    0 赞同 0 条回复
  • Mooby 普通 2023年7月12日 下午4:00

    六
    /**
    * Definition for a binary tree node.
    * public class TreeNode {
    * int val;
    * TreeNode left;
    * TreeNode right;
    * TreeNode(int x) { val = x; }
    * }
    */
    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 left = maxDepth(root.left);
    if (left
    -1) {
    return -1;
    }
    int right = maxDepth(root.right);
    if (right -1)
    return -1;
    if (Math.abs(left – right) > 1)
    return -1;
    return Math.max(left, right) + 1;
    }
    }

    0 赞同 0 条回复

# 回答此问题

后才能回答