面试题55 - II. 平衡二叉树(110. 平衡二叉树)(Java)

1 题目

https://leetcode-cn.com/problems/balanced-binary-tree/
在这里插入图片描述

2 Java

2.1 方法一(两层递归)

先写出求某个节点的层数的方法layerNum,这是第一层递归
再写出求某个节点是否平衡的方法isBalanced,这是第二层递归
某节点层数 = max(左节点层数,右节点层数)+ 1
某节点平衡 = 本身平衡 & 左节点平衡 & 右节点平衡

/**
 * 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;
        return (Math.abs(height(root.left) - height(root.right)) <= 1) && isBalanced(root.left) && isBalanced(root.right);
    }
    public int height(TreeNode root){
        if(root == null)    return 0;
        return Math.max(height(root.left), height(root.right)) + 1;
    }
}

2.2 方法二(递归优化,建备忘录)

用HashMap记录每个节点高度,但是运行速度变慢了,不知道什么情况

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    // 建立备忘录
    HashMap<TreeNode, Integer> treeMap = new HashMap<TreeNode, Integer>();

    public boolean isBalanced(TreeNode root) {
        if(root == null)    return true;
        return  (Math.abs(height(root.left) - height(root.right)) <= 1) && isBalanced(root.left) && isBalanced(root.right);
    }

    public int height(TreeNode root){
        if(root == null) return 0;
        if(treeMap.containsKey(root))   return treeMap.get(root);

        int ans = Math.max(height(root.left), height(root.right)) + 1;
        treeMap.put(root, ans);
        return ans;
    }
}

2.3 方法三(递归再改进)

如果非平衡,返回-1,不再判断了

/**
 * 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(height(root) == -1)  return false;
        return true;
    }
    public int height(TreeNode root){
        if(root == null)    return 0;
        int left = height(root.left);
        int right = height(root.right);
        if(left == -1 || right == -1 || Math.abs(left - right) > 1)   return -1;
        return Math.max(left, right) + 1;
    }
}

3 二刷

3.1 方法一(借助成员变量)

用一个成员变量记录是否存在非平衡的子树,这样只需要求一次树高

/**
 * 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) {
        height(root);
        return flag;
    }

    // 用一个成员变量记录是否存在非平衡的子树
    boolean flag = true;    
    public int height(TreeNode root){
        if(root == null)    return 0;

        int lH = height(root.left);
        int rH = height(root.right);
        int H = Math.max(lH, rH) + 1;

        // 仅仅多了这一步!!!记录是否为平衡树
        if(Math.abs(lH - rH) > 1)   flag = false;

        // 依然返回树高
        return H;
    }
}

4 三刷

4.1 方法一(借助特殊返回值 -1)

/**
 * 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) {
        int h = height(root);
        return h == -1 ? false : true;
    }
    // 约等于求二叉树高度/深度,仅多了一行;面试题55 - I. 二叉树的深度(104. 二叉树的最大深度)
    public int height(TreeNode root){
        if(root == null)    return 0;

        int lH = height(root.left);
        int rH = height(root.right);
        
        // 仅多这一行,利用返回特殊值 -1
        if(lH == -1 || rH == -1 || Math.abs(lH - rH) > 1)   return -1;
        
        int h = Math.max(lH, rH) + 1;
        return h;
    }
}

4.2 方法二(借助成员变量)

/**
 * 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) {
        height(root);
        return res;
    }

    public boolean res = true;
    // 约等于求二叉树高度/深度,仅多了一行;面试题55 - I. 二叉树的深度(104. 二叉树的最大深度)
    public int height(TreeNode root){
        if(root == null)    return 0;

        int lH = height(root.left);
        int rH = height(root.right);
        // 仅多这一行,利用成员变量记录
        if(Math.abs(lH - rH) > 1)   res = false;
        int h = Math.max(lH, rH) + 1;
        return h;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值