当前位置:网站首页>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;
}
}
边栏推荐
- Day108.尚医通:医院模拟系统接口对接 - 医院|科室|排班 增删改分页条件查询
- Day 11 notes
- How many processors is Tianji 820 equivalent to Xiaolong? How about Tianji 1100 equivalent to Xiaolong? How about Tianji 820
- Stream stream is used for classification display.
- 酷睿i5 12490f和i5 12600k差距大吗
- 鸡与蛋,产品与策略
- pingbanceshi
- kafka消费报错coordinator unavailable.Rediscovery will be attempt redisCovery
- 过程块和方法
- fastadmin更改默认表格按钮的弹窗大小
猜你喜欢

达人评测 酷睿i9 12950hx和i9 12900hx区别哪个强

酷睿i7 1165g7相当于什么水平 i71165g7属于哪个档次

What is Tianji 920 equivalent to a snapdragon? How much is Tianji 920 equivalent to a snapdragon? How about Tianji 920

Comparison of iqoo 10 pro and Xiaomi 12 ultra configurations

How many processors is Tianji 820 equivalent to Xiaolong? How about Tianji 1100 equivalent to Xiaolong? How about Tianji 820

STM32 output sine wave +cubemx configuration +hal Library

Day 11 notes

Review of HCIA

The difference between Celeron n4000 and Celeron n5095

Day 8 notes
随机推荐
Day 8 notes
Static comprehensive experiment (HCIA)
338. 比特位计数
强化学习——策略梯度理解点
152. 乘积最大子数组
Overlayfs source code parsing
What level is rtx3090ti? What level is rtx3090ti graphics card? How about rtx3090ti graphics card
将我理解的web3.0讲给你听
Which is the difference between iqoo 10 pro and vivo X80 pro? Detailed parameter configuration comparison
How many processors is Tianji 720 equivalent to Xiaolong? How about Tianji 720 equivalent to Xiaolong
Stream stream is used for classification display.
太平洋大西洋水流问题
Design instantiation and connection
笔记本酷睿i5 1135g7相当于什么水平?i5 1135g7性能怎么样
赛扬n5095处理器怎么样 英特尔n5095核显相当于什么水平
Description of test platform and hardware design
OSPF综合实验
Tensor、Numpy、PIL格式转换以及图像显示
Day108. Shang Yitong: interface docking of hospital simulation system - query of hospital | Department | shift scheduling, addition, deletion, modification and paging conditions
Life essays: annoying projects in 2022