代码随想录四刷day15

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • [一、力扣100. 相同的树](#一、力扣100. 相同的树)
  • [二、力扣572. 另一棵树的子树](#二、力扣572. 另一棵树的子树)
  • [三、力扣104. 二叉树的最大深度](#三、力扣104. 二叉树的最大深度)
  • [四、力扣559. N 叉树的最大深度](#四、力扣559. N 叉树的最大深度)

前言


最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

一、力扣100. 相同的树

递归

java 复制代码
/**
 * 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 isSameTree(TreeNode p, TreeNode q) {
        return fun(p,q);
    }
    public boolean fun(TreeNode p, TreeNode q){
        if(p == null && q == null){
            return true;
        }
        if(p == null || q == null){
            return false;
        }
        if(p.val != q.val){
            return false;
        }
        boolean b1 = fun(p.left, q.left);
        boolean b2 = fun(p.right,q.right);
        return b1 && b2;
    }
}

迭代

java 复制代码
/**
 * 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 isSameTree(TreeNode p, TreeNode q) {
        Deque<TreeNode> deq = new LinkedList<>();
        deq.offerLast(p);
        deq.offerLast(q);
        while(!deq.isEmpty()){
            TreeNode t1 = deq.pollFirst();
            TreeNode t2 = deq.pollFirst();
            if(t1 == null && t2 == null){
                continue;
            }
            if(t1 == null || t2 == null){
                return false;
            }
            if(t1.val != t2.val){
                return false;
            }
            deq.offerLast(t1.left);
            deq.offerLast(t2.left);
            deq.offerLast(t1.right);
            deq.offerLast(t2.right);
        }
        return true;
    }
}

二、力扣572. 另一棵树的子树

java 复制代码
/**
 * 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 isSubtree(TreeNode root, TreeNode subRoot) {
        return postOrder(root,subRoot);
    }
    public boolean postOrder(TreeNode root, TreeNode subRoot){
        if(root == null){
            return false;
        }
        boolean b1 = postOrder(root.left,subRoot);
        if(b1){
            return b1;
        }
        boolean b2 = postOrder(root.right,subRoot);
        if(b2){
            return b2;
        }
        boolean b3 = fun(root,subRoot);
        return b1 || b2 || b3;
    }
    public boolean fun(TreeNode p, TreeNode q){
        if(p == null && q == null){
            return true;
        }
        if(p == null || q == null){
            return false;
        }
        if(p.val != q.val){
            return false;
        }
        boolean b1 = fun(p.left,q.left);
        boolean b2 = fun(p.right,q.right);
        return b1 && b2;
    }
}

三、力扣104. 二叉树的最大深度

java 复制代码
/**
 * 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) {
        return fun(root);
    }
    public int fun(TreeNode root){
        if(root == null){
            return 0;
        }
        int l = fun(root.left);
        int r = fun(root.right);
        return l > r ? l + 1 : r + 1;
    }
}

四、力扣559. N 叉树的最大深度

java 复制代码
/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public int maxDepth(Node root) {
        return fun(root);
    }
    public int fun(Node root){
        if(root == null){
            return 0;
        }
        int max = 0;
        for(Node node : root.children){
            max = Math.max(max,fun(node));
        }
        return max + 1;
    }
}
相关推荐
被AI抢饭碗的人8 分钟前
算法题(150):拼数
数据结构·算法
知识漫步14 分钟前
代码随想录算法训练营第60期第三十九天打卡
算法
June`17 分钟前
专题五:floodfill算法(太平洋大西洋水流问题)
c++·算法·leetcode·深度优先·剪枝
苦学编程的谢28 分钟前
多线程进阶
java·开发语言·java-ee
Uranus^36 分钟前
深入解析Java微服务架构:Spring Boot与Spring Cloud的整合实践
java·spring boot·spring cloud·微服务·分布式系统
牛马baby1 小时前
Java高频面试之并发编程-18
java·开发语言·面试
蒂法就是我1 小时前
Spring的后置处理器是干什么用的?扩展点又是什么?
java·后端·spring
CodeLinghu1 小时前
宝塔面板部署前后端项目SpringBoot+Vue2
java·spring boot·后端
悟能不能悟1 小时前
Spring Boot循环依赖的陷阱与解决方案:如何打破“Bean创建死循环”?
java·spring boot·spring
Zero two and hiro1 小时前
tomcat一闪而过,按任意键继续以及控制台中文乱码问题
java·服务器·tomcat