代码随想录|226.翻转二叉树 101. 对称二叉树104.二叉树的最大深度 111.二叉树的最小深度

import javax.print.attribute.standard.MediaSize;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Day12 {
    private class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

//    给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。
    //递归法
    public TreeNode invertTree1(TreeNode root) {
        if (root==null)
            return null;
        TreeNode store=root.left;
        root.left=root.right;
        root.right=store;
        invertTree1(root.left);
        invertTree1(root.right);
        return root;
    }
    //迭代法:前序遍历(不仅前中后序遍历可以,层序遍历也可以)
    public TreeNode invertTree2(TreeNode root) {
        if (root==null)
            return null;
        Stack<TreeNode> stack=new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode cur=stack.pop();

            TreeNode store=cur.left;
            cur.left=cur.right;
            cur.right=store;//交换左右节点

            if(cur.right!=null)
                stack.push(cur.right);
            if(cur.left!=null)
                stack.push(cur.left);
        }
        return root;
    }
    //层序遍历
    public TreeNode invertTree3(TreeNode root) {
        if (root==null)
            return null;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            TreeNode cur=queue.poll();

            TreeNode store=cur.left;
            cur.left=cur.right;
            cur.right=store;//交换左右节点

            if(cur.left!=null)
                queue.add(cur.left);
            if (cur.right!=null)
                queue.add(cur.right);

        }
        return root;
    }
    //给你一个二叉树的根节点 root , 检查它是否轴对称。
    public boolean isSymmetric(TreeNode root) {
        return compare(root.left,root.right);
    }
    public boolean compare(TreeNode left,TreeNode right){
        if(left==null&&right!=null)
            return false;
        else if(left!=null&&right==null)
            return false;
        else if (left==null&&right==null) {
            return true;
        }
        else if (left.val!=right.val){
            return false;
        }
        boolean inside=compare(left.right,right.left);//比较内侧叶子
        boolean outside=compare(left.left,right.right);//比较外侧叶子
        boolean result=inside&&outside;
        return result;
    }
    //给定一个二叉树 root ,返回其最大深度。
    //二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
    public int maxDepth(TreeNode root) {
        if (root==null)
            return 0;
        //使用后序遍历
        int leftDepth=maxDepth(root.left);//左
        int rightDepth=maxDepth(root.right);//右
        int Depth= Math.max(leftDepth,rightDepth)+1;//中
        return Depth;
    }
    //给定一个二叉树,找出其最小深度。
    //最小深度是从根节点到最近叶子节点的最短路径上的节点数量
    //说明:叶子节点是指没有子节点的节点。
    public int minDepth(TreeNode root) {
        if(root==null)
            return 0;
        int leftDepth=minDepth(root.left);
        int rightDepth=minDepth(root.right);
        //最小深度和最大深度不一样,最小深度要找叶子节点  不能将null节点当作叶子节点
        if(root.left!=null&&root.right==null){
            return 1+leftDepth;
        }
        if(root.right!=null&&root.left==null){
            return 1+rightDepth;
        }
        int depth= Math.min(leftDepth,rightDepth)+1;
        return depth;
    }
    public int minDepth2(TreeNode root) {//层序遍历
        Deque<TreeNode> queue=new LinkedList<>();
        int depth=0;
        if(root!=null)
            queue.add(root);
        else
            return depth;
        while(!queue.isEmpty()){
            depth++;
            int size=queue.size();//层序遍历一定要提前将size拿出来,不然size在for循环中是个变量
            for(int i=0;i<size;i++){
                TreeNode node=queue.poll();
                if(node.left!=null)
                    queue.add(node.left);
                if(node.right!=null)
                    queue.add(node.right);
                if(node.left==null&&node.right==null)
                    return depth;
            }
        }
        return depth;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值