当前位置: 首页 > news >正文

代码随想录算法刷题训练营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;}
}
http://www.lryc.cn/news/288855.html

相关文章:

  • 【C语言/数据结构】排序(直接插入排序|希尔排序)
  • Jupyter Notebook安装使用教程
  • Unity 中的接口和继承
  • C++区间覆盖(贪心算法)
  • Python with Office 054 - Work with Word - 7-9 插入图像 (3)
  • Nodejs前端学习Day4_fs文件系统模块基础应用之成绩转换
  • 五、Kotlin 函数进阶
  • 重温《深入理解Java虚拟机:JVM高级特性与最佳实践(第二版)》 –– 学习笔记(一)
  • 定向减免!函数计算让轻量 ETL 数据加工更简单,更省钱
  • git checkout和git switch的区别
  • 故障树分析蒙特卡洛仿真程序(附MATLAB完整代码)
  • 数据结构-线性表
  • java金额数字转中文
  • Ubuntu findfont: Font family ‘SimHei‘ not found.
  • mysql小知识
  • Unity中URP下逐顶点光照
  • Spring Boot3整合Druid(监控功能)
  • 使用Gin框架,快速开发高效的Go Web应用程序
  • 【Unity】【游戏开发】Pico打包后项目出现运行时错误如何Debug
  • 一种解决常用存储设备无法被电脑识别的方法
  • Spark运行架构以及容错机制
  • 短剧APP小程序源码 全开源短视频系统源码/h5/app/小视频系统
  • 深度学习中图像分类、目标检测、语义分割、实例分割哪个难度大,哪个检测精度容易实现,哪个速度低。请按照难度、精度容易实现程度、速度排名。
  • 【AI视野·今日NLP 自然语言处理论文速览 第七十五期】Thu, 11 Jan 2024
  • 数据结构:搜索二叉树 | 红黑树 | 验证是否为红黑树
  • 数据结构顺序表
  • 手把手教你优雅的安装虚拟机 Ubuntu —— 图文并茂
  • 源 “MySQL 5.7 Community Server“ 的 GPG 密钥已安装,但是不适用于此软件包。请检查源的公钥 URL 是否配置正确。
  • springboot核心有几层架构
  • css3表格练习