Leetcode——对称二叉树 / 平衡二叉树

1 对称二叉树

给定一个二叉树,检查它是否是镜像对称的。

例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
在这里插入图片描述
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
在这里插入图片描述

(1)解法一: 递归

思路简单:

  • 首先排除null情况
  • 判断左子树的右节点 = 右子树左节点
  • 判断左子树的左节点 = 右子树右节点
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root == null)
            return true;
        return help(root.left, root.right);
    }

    public boolean help(TreeNode left, TreeNode right){
        if(left == null && right == null)
            return true;
        if(left == null || right == null || left.val != right.val)
            return false;
        return help(left.left, right.right) && help(left.right, right.left);
    }
}

(2)解法二: 非递归

原理其实一样,注意:

  • 如果都为空继续循环,考虑另外节点的左右节点可能对称
    if (left == null && right == null)
    continue;
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
    //队列
    Queue<TreeNode> queue = new LinkedList<>();
    if (root == null)
        return true;
    //左子节点和右子节点同时入队
    queue.add(root.left);
    queue.add(root.right);

    //如果队列不为空就继续循环
    while (!queue.isEmpty()) {
        //每次两个出队
        TreeNode left = queue.poll(), right = queue.poll();

        //如果都为空继续循环,考虑另外节点的左右节点可能对称
        if (left == null && right == null)
            continue;		

        //如果一个为空一个不为空,说明不是对称的,直接返回false
        if (left == null || right == null)
            return false;

        //如果这两个值不相同,也不是对称的,直接返回false
        if (left.val != right.val)
            return false;
        
        //这里要记住入队的顺序,他会每两个两个的出队。
        //左子节点的左子节点和右子节点的右子节点同时
        //入队,因为他俩会同时比较。
        //左子节点的右子节点和右子节点的左子节点同时入队,
        //因为他俩会同时比较
        queue.add(left.left);
        queue.add(right.right);
        queue.add(left.right);
        queue.add(right.left);
    }
    return true;
}
}

2 平衡二叉树

在这里插入图片描述

(1)从顶至底(暴力法)

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

(2)从底至顶(提前阻断)

  • 思路是对二叉树做先序遍历,从底至顶返回子树最大高度,若判定某子树不是平衡树则 “剪枝” ,直接向上返回。
class Solution {
    public boolean isBalanced(TreeNode root) {
        return recur(root) != -1;
    }

    private int recur(TreeNode root) {
        if (root == null) return 0;
        
        int left = recur(root.left);
        if(left == -1) return -1;
        
        int right = recur(root.right);
        if(right == -1) return -1;
        
        return Math.abs(left - right) < 2 ? Math.max(left, right) + 1 : -1;
    }
}

(3)迭代

class Solution {
   /**
     * 迭代法,效率较低,计算高度时会重复遍历
     * 时间复杂度:O(n^2)
     */
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre = null;
        while (root!= null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            TreeNode inNode = stack.peek();
            // 右结点为null或已经遍历过
            if (inNode.right == null || inNode.right == pre) {
                // 比较左右子树的高度差,输出
                if (Math.abs(getHeight(inNode.left) - getHeight(inNode.right)) > 1) {
                    return false;
                }
                stack.pop();
                pre = inNode;
                root = null;// 当前结点下,没有要遍历的结点了
            } else {
                root = inNode.right;// 右结点还没遍历,遍历右结点
            }
        }
        return true;
    }

(4)层序遍历

class Solution {
    //层序遍历
    private void travel(TreeNode node ,int level,List high){
        if(node == null){return;}
        int size = high.size();
        if(size<=level){
            high.add(1);
        }
        travel(node.left,level+1,high);
        travel(node.right,level+1,high);
    }
    //通过层序遍历求高度
    private int gethigh(TreeNode node){
        List<Integer> high = new ArrayList<Integer>();
        travel(node,0,high);
        return high.size();
    }

    public boolean isBalanced(TreeNode root) {
        if(root == null){return true;}
        int lHigh = gethigh(root.left);
        int rHigh = gethigh(root.right);
        //一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 
        //1.root是平衡的
        if(Math.abs(lHigh-rHigh) > 1){
            return false;
        }else{//2.root.left和root.right都是平衡的
            //如何进行递归让我思考了10分钟。。。
            boolean res = true;
            res = isBalanced(root.left) && isBalanced(root.right);
            return res;
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yawn__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值