当前位置:网站首页>104 二叉树的最大深度 和 543 二叉树的直径和 124 二叉树的最大路径和
104 二叉树的最大深度 和 543 二叉树的直径和 124 二叉树的最大路径和
2022-07-23 08:03:00 【ATTACH_Fine】
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;
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left,right) + 1;
}
}
543二叉树的直径和
题目
给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。
示例:
代码
/** * 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 res = 0;
public int diameterOfBinaryTree(TreeNode root) {
dfs(root);
return res-1;
}
public int dfs(TreeNode root){
if(root == null) return 0;
int l = dfs(root.left);
int r = dfs(root.right);
res = Math.max(res,r+l+1);
return Math.max(l,r)+1;
}
}
124 二叉树的最大路径和
题目
路径 被定义为一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。同一个节点在一条路径序列中 至多出现一次 。该路径 至少包含一个 节点,且不一定经过根节点。
路径和 是路径中各节点值的总和。
给你一个二叉树的根节点 root ,返回其 最大路径和 。
示例:
代码
/** * 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 res = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
getMax(root);
return res;
}
public int getMax(TreeNode root){
if(root == null) return 0;
int l = Math.max(0,getMax(root.left));
int r = Math.max(0,getMax(root.right));
res = Math.max(res,root.val+l+r);
return Math.max(l,r) + root.val;
}
}
边栏推荐
- 中等靶场
- MGRE comprehensive experiment
- 第十天笔记
- MySQL enables scheduled task execution
- Day108.尚医通:医院模拟系统接口对接 - 医院|科室|排班 增删改分页条件查询
- Pbootcms数据库转换教程(sqlite转mysql详细教程)
- 小米12S Pro和小米12Pro天玑版区别 两者配置对比
- What level is rtx3090ti? What level is rtx3090ti graphics card? How about rtx3090ti graphics card
- 激励发生器、监测器
- mysql开启定时调度任务执行
猜你喜欢

记一次Vulnhub靶机练习成功拿下root权限

iQOO 10 Pro和小米12 Ultra哪个好 哪个值得买 两者配置对比

第九天笔记

How about Celeron n5095 Processor? What is the equivalent level of Intel n5095 core display

Rtx3080ti and rtx3080 gap 3080 and 3080ti parameter comparison

How about the nuclear display performance of Ruilong R7 Pro 6850h? What level is it equivalent to

Is machine learning difficult to get started? Tell me how I started machine learning quickly!

第六天笔记

ERP production operation control

MGRE comprehensive experiment
随机推荐
第五天筆記
How about the performance of Ruilong R7 Pro 5875u? What level is it equivalent to
Le shell a besoin de connaître les commandes
Review of HCIA
Rtx3080ti and rtx3080 gap 3080 and 3080ti parameter comparison
How many processors is Tianji 720 equivalent to Xiaolong? How about Tianji 720 equivalent to Xiaolong
STM32 output sine wave +cubemx configuration +hal Library
酷睿i5 12490f和i5 12600k差距大吗
锐龙R7 PRO 6850H核显性能怎么样?相当于什么水平
js 实现随机生成UUID
JS数据类型判断方式总结
第七天笔记
拖拽----
Surrounded Regions
rtx3080ti和3090差距 rtx3080ti和3090哪个性价比高
Notes on key vocabulary of the original English book biography of jobs (15) [chapter four]
Is machine learning difficult to get started? Tell me how I started machine learning quickly!
The shell needs to know the commands when running
考研题库小程序中如何实现打开考研思维导图pdf
Spark counts new users every day