代码随想录算法刷题训练营day16

代码随想录算法刷题训练营day16:LeetCode(104)二叉树的最大深度 、LeetCode(559)n叉树的最大深度、LeetCode(111)二叉树的最小深度、LeetCode(222)完全二叉树的节点个数

LeetCode(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) {
   
        //通过递归去做---传入的是根节点,可以求根节点的高度来代替深度
        //先求左右子树的高度+1即为根节点的高度----确定遍历方式为左右根
        //采用递归,先判断终止条件
        if(root==null){
   
            return 0;
        }
        //不为空,先求左子树的高度
        int heightLeft=maxDepth(root.left);
        //再求右子树的高度
        int heightRight=maxDepth(root.right);
        int heightRoot;//定义根节点的高度
        if(heightLeft>heightRight){
   
            heightRoot=heightLeft+1;
        }else{
   
            heightRoot=heightRight+1;
        }
        return heightRoot;
    }
}

LeetCode(559)n叉树的最大深度
题目
在这里插入图片描述
代码

// @lc code=start
/*
// 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 maxheight=0;
        List<Node> childrens=root.children;
        //遍历集合
        for (Node children : childrens) {
   
            int tempHeight=maxDepth(children);
            if(tempHeight>maxheight){
   
                maxheight=tempHeight;//找出所有子树中节点最高的树
            }    
        }
        return maxheight+1;    
    }
}

LeetCode(111)二叉树的最小深度
题目
在这里插入图片描述

代码

// @lc code=start
/**
 * 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) {
   
        if(root==null){
   
            return 0;
        }
        int rootHeight;
        //同样用后续遍历去做
        if(root.left==null){
   
             rootHeight=minDepth(root.right)+1;
             return rootHeight;
        }
        if(root.right==null){
   
             rootHeight=minDepth(root.left)+1;
             return rootHeight;
        }
        int leftHeight=minDepth(root.left);
        int rightHeight=minDepth(root.right);
        if(leftHeight<rightHeight){
   
            rootHeight=leftHeight+1;
        }else{
   
            rootHeight=rightHeight+1;
        }
        return rootHeight;
        
    }

LeetCode(222)完全二叉树的节点个数
题目
在这里插入图片描述
代码

// @lc code=start
/**
 * 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 {
   
    int number=0;
    public int countNodes(TreeNode root) {
   
        if(root==null){
   
            return 0;
        }
        int leftCountNodes=countNodes(root.left);
        int rightCountNodes=countNodes(root.right);
        int countSum=leftCountNodes+rightCountNodes+1;//常规遍历方法
        return countSum;
    }
}

相关推荐

  1. 代码随想算法训练|day17

    2024-01-28 12:38:01       45 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-28 12:38:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-28 12:38:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-28 12:38:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-28 12:38:01       18 阅读

热门阅读

  1. 【算法题】72. 编辑距离

    2024-01-28 12:38:01       23 阅读
  2. LLVM编译器的结构

    2024-01-28 12:38:01       33 阅读
  3. 计算机网络概述及 参考模型

    2024-01-28 12:38:01       31 阅读
  4. 关于CMAKE构建C/C++遇到的问题汇总

    2024-01-28 12:38:01       35 阅读
  5. 栈的基础知识

    2024-01-28 12:38:01       23 阅读
  6. perl 通过信号控制执行超时

    2024-01-28 12:38:01       30 阅读
  7. 设计模式 :总结篇

    2024-01-28 12:38:01       36 阅读
  8. Spring Cloud Sleuth与Zipkin详解

    2024-01-28 12:38:01       41 阅读
  9. Python在网络安全防御中的应用与实践

    2024-01-28 12:38:01       35 阅读
  10. @Scheduled笔记240124

    2024-01-28 12:38:01       31 阅读