代码随想录二刷day16

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

文章目录

  • 前言
  • [一、力扣104. 二叉树的最大深度](#一、力扣104. 二叉树的最大深度)
  • [二、力扣559. N 叉树的最大深度](#二、力扣559. N 叉树的最大深度)
  • [三、力扣111. 二叉树的最小深度](#三、力扣111. 二叉树的最小深度)
  • [三、力扣力扣222. 完全二叉树的节点个数](#三、力扣力扣222. 完全二叉树的节点个数)

前言


一、力扣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) {
        if(root == null){
            return 0;
        }
        int l = maxDepth(root.left);
        int r = maxDepth(root.right);
        return l > r ? l + 1 : r + 1;
    }
}

迭代

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) {
        Deque<TreeNode> deq = new LinkedList<>();
        if(root == null)return 0;
        deq.offerLast(root);
        int high = 0;
        while(!deq.isEmpty()){
            int len = deq.size();
            for(int i = 0; i < len; i ++){
                TreeNode p = deq.pollFirst();
                if(p.left!= null)deq.offerLast(p.left);
                if(p.right != null)deq.offerLast(p.right);
            }
            high ++;
        }
        return high;
    }
}

二、力扣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) {
        Deque<Node> deq = new LinkedList<>();
        if(root == null)return 0;
        deq.offerLast(root);
        int high = 0;
        while(!deq.isEmpty()){
            int len = deq.size();
            for(int i = 0; i < len; i ++){
                Node p = deq.pollFirst();
                List<Node> li = p.children;
                for(Node n : li){
                    if(n != null){
                        deq.offerLast(n);
                    }
                }
            }
            high ++;
        }
        return high;
    }
}

递归

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) {
        if(root == null)return 0;
        int[] arr = new int[root.children.size()];
        int max = 0;
        for(int i = 0; i < arr.length; i ++){
            arr[i] = maxDepth(root.children.get(i));
            max = max > arr[i] ? max : arr[i];
        }
        return max + 1;
    }
}

三、力扣111. 二叉树的最小深度

迭代

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 minDepth(TreeNode root) {
        Deque<TreeNode> deq = new LinkedList<>();
        if(root == null)return 0;
        deq.offerLast(root);
        int depth = 0;
        while(!deq.isEmpty()){
            int len = deq.size();
            for(int i = 0; i <len ; i ++){
                TreeNode p = deq.pollFirst();
                if(p.left == null && p.right == null){
                    return depth + 1;
                }
                if(p.left != null)deq.offerLast(p.left);
                if(p.right != null)deq.offerLast(p.right);
            }
            depth ++;
        }
        return depth;
    }
}

递归

java 复制代码
class Solution {
    /**
     * 递归法,相比求MaxDepth要复杂点
     * 因为最小深度是从根节点到最近**叶子节点**的最短路径上的节点数量
     */
    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftDepth = minDepth(root.left);
        int rightDepth = minDepth(root.right);
        if (root.left == null) {
            return rightDepth + 1;
        }
        if (root.right == null) {
            return leftDepth + 1;
        }
        // 左右结点都不为null
        return Math.min(leftDepth, rightDepth) + 1;
    }
}

三、力扣力扣222. 完全二叉树的节点个数

迭代

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 countNodes(TreeNode root) {
        Deque<TreeNode> deq = new LinkedList<>();
        if(root == null)return 0;
        deq.offerLast(root);
        int count = 0;
        while(!deq.isEmpty()){
            int len = deq.size();
            for(int i = 0; i < len ; i ++){
                TreeNode p = deq.pollFirst();
                count ++;
                if(p.left != null)deq.offerLast(p.left);
                if(p.right != null)deq.offerLast(p.right);
            }
        }
        return count;
    }
}

递归

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 countNodes(TreeNode root) {
        if(root == null)return 0;
        int l = countNodes(root.left);
        int r = countNodes(root.right);
        return l + r + 1;
    }
}
相关推荐
陈大爷(有低保)9 分钟前
UDP Socket聊天室(Java)
java·网络协议·udp
秋夫人22 分钟前
B+树(B+TREE)索引
数据结构·算法
kinlon.liu23 分钟前
零信任安全架构--持续验证
java·安全·安全架构·mfa·持续验证
王哲晓44 分钟前
Linux通过yum安装Docker
java·linux·docker
java6666688881 小时前
如何在Java中实现高效的对象映射:Dozer与MapStruct的比较与优化
java·开发语言
Violet永存1 小时前
源码分析:LinkedList
java·开发语言
执键行天涯1 小时前
【经验帖】JAVA中同方法,两次调用Mybatis,一次更新,一次查询,同一事务,第一次修改对第二次的可见性如何
java·数据库·mybatis
代码雕刻家1 小时前
数据结构-3.1.栈的基本概念
c语言·开发语言·数据结构
梦想科研社1 小时前
【无人机设计与控制】四旋翼无人机俯仰姿态保持模糊PID控制(带说明报告)
开发语言·算法·数学建模·matlab·无人机
Milo_K1 小时前
今日 leetCode 15.三数之和
算法·leetcode