二叉树相关习题复习2020/1/18

这篇博客回顾了LeetCode中关于二叉树的五道经典习题,包括相同的树(100)、对称二叉树(101)、最大深度(104)、层序遍历(107)和最小深度(111)。重点讲解了递归解题思路和BFS、DFS的运用,强调了递归分类和算法优化的重要性。

习题均来自LeetCode


中难题我唯唯诺诺简单题我重拳出击嘻嘻

(1)相同的树 题号–100

这一题十分简单没啥好说的
就简单的递归遍历判断每个结点是否相同(下图:耗时+消耗内存)
在这里插入图片描述

在这里插入图片描述

/**
 * 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 {
    boolean viya=true;
    public boolean isSameTree(TreeNode p, TreeNode q) {
            if(p==null & q==null){
                return true;
            }
             if((p==null && q!=null) || (p!=null && q==null) || (p.val!=q.val)){
                return false;
            }
            two(p,q);
            return this.viya;
    }
    public void two(TreeNode p,TreeNode q){
        if((p==null && q!=null) || (p!=null && q==null)){
                this.viya=false;
                return;
        }
        if(p==null && q==null){
            return;
        }
        two(p.left,q.left);
        if(p.val!=q.val){
            this.viya=false;
            return; 
        }
        two(p.right,q.right);
    }
}

(2)对称二叉树 题号–101

根100题是类似的 需要注意的是镜像!!!
在这里插入图片描述
在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    boolean baby=true;
    public boolean isSymmetric(TreeNode root) {
        if(root==null){
            return true;
        }
        if((root.left==null && root.right!=null)|| (root.left!=null && root.right==null)){
            return false;
        }
        two(root.left,root.right);
        return this.baby;
    }
    public void two(TreeNode p,TreeNode q){
        if(p==null && q==null){
            return;
        }else if(p!=null && q==null || p==null && q!=null){
            this.baby=false;
            return;
        }
        two(p.left,q.right);
        if(p.val!=q.val){
           this.baby=false;
           return; 
        }
        two(p.right,q.left);
    }
}

(3)二叉树的最大深度 题号–104

这一题在上几篇博客中已经实现过了
就是简单的递归就完事了

/**
 * 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 int maxDepth(TreeNode root) {
        if(root == null)return 0;
        return searchMax(root,0);
    }
    public int searchMax(TreeNode root,int Max){
        if(root == null){
            return Max;
        }
        int LeftMax=searchMax(root.left,Max+1);
        int RightMax=searchMax(root.right,Max+1);
        return LeftMax > RightMax ? LeftMax : RightMax;
    }                                      
}

(4)二叉树的层序遍历 题号–107

这一题是比较重要的
顺便可以去学习以下什么是DFS(深度优先遍历)和BFS(广度优先遍历)

以下的操作是这样的:首先需要一个队列去存储每层的元素的val
还有一个队列去存储树的每层的结点
接下来的操作就是 出列 和 入列
如图操作:
在这里插入图片描述
题目如下:
在这里插入图片描述
在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        LinkedList<List<Integer>> list=new LinkedList<>();
        if(root==null){
            return list;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            List<Integer> val=new LinkedList<>();
            int count=queue.size();
            for(int i=0;i<count;i++){
                TreeNode node=queue.poll();
                val.add(node.val);
                if(node.left!=null){
                    queue.add(node.left);
                }
                if(node.right!=null){
                    queue.add(node.right);
                }
            }
            list.addFirst(val);
        }
        return list;
    }
}

(5)二叉树的最小深度 题号–111

这一题呢 :在算法的区分度上就比较高我觉得
需要你进行算法优化,比如剪枝是一个手段
还可以层次遍历 遇到叶子直接返回 这种是最高效的
话不多说看代码 :(注意 是count+1 不是count++ 这为了保证回溯的时候不改变当前的count是没有被改变的,相当于你返回那一此递归count就是当前的深度)
在这里插入图片描述
在这里插入图片描述

/**
 * 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;
 *     }
 * }
 */
 /*
 	以下是我的写法  下面还有0ms的模板  你们看看  剪枝具有一定运气成分(我指的是提高的效率)原因你自己去思考,我不多说,如果不明白可以私信。(我也是菜逼)
 */
class Solution {
    public int min = Integer.MAX_VALUE;
    public int minDepth(TreeNode root) {
        if(root==null)return 0;
        viya(root,1);
        return this.min;
    }
    public void viya(TreeNode root,int count){
        if(root==null)return;
        if(root.left==null && root.right==null){
            min=count<=min?count:min;
        }
        if(count>=min)return;
        viya(root.left,count+1);
        viya(root.right,count+1);
    }
}
/**
 * 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;
 *     }
 * }
 */
 /*
 		以下是高手写的0ms模板
 */
class Solution {
    public int minDepth(TreeNode root) {

        // 深度优先
        if(root == null) {
            return 0;
        }
        LinkedList<TreeNode> treeNode = new LinkedList<TreeNode>();
        treeNode.offer(root);
        return minDepth1(treeNode,1);
    }
    public int minDepth1(LinkedList<TreeNode> linkedList, int depth){
        // 广度优先
        if(linkedList.isEmpty()) {
            return depth;
        }
        int size = linkedList.size();
        while (size>0) {
            TreeNode treeNode = linkedList.poll();
            if (treeNode.left == null && treeNode.right == null) {
                return depth;
            }
            if(treeNode.left != null) {
                linkedList.offer(treeNode.left);
            }
            if(treeNode.right != null) {
                linkedList.offer(treeNode.right);
            }
            size--; 
        }
        return minDepth1(linkedList,depth+1);
    }
}

总结:

2021/1/18练习题
前面三个题我认为是需要快速 秒杀的题
对递归思路要相当清晰
树这块还是很有意思的!!可以让你更加深层次理解递归和回溯
递归分类的话可以这样分:(难度递增)
1.不带返回值,不带额外参数
2.不带返回值,带额外参数
3.带返回值,带额外参数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值