当前位置:网站首页>leetcode二叉树系列(二)
leetcode二叉树系列(二)
2022-08-04 09:03:00 【你食不食油饼】
1、二叉树展开为链表
题目描述:

1)暴力拼接
思路:就是遍历每一个节点root,找到当前节点左子树的最右节点pre,
pre.right = root.right
root.right = root.left
root.left = null
这样遍历每一个节点,就可以去掉所有节点的左子树
代码如下:
public void flatten(TreeNode root) {
TreeNode pre;
while (root!=null){
//左子树为空,则跳过
if (root.left == null) {
root = root.right;
} else {
pre = root.left;
//找到当前节点左子树的最右节点
while (pre.right != null) pre = pre.right;
pre.right = root.right;
root.right = root.left;
root.left = null;
root = root.right;
}
}2)先序遍历拼接
思路:根据先序遍历的顺序依次,把左子树拼接到右子树
代码如下:
TreeNode pre;
public void flatten(TreeNode root) {
if(root == null) return;
TreeNode left = root.left;
TreeNode right = root.right;
root.left = null;
if(pre != null) pre.right = root;
pre = root;
flatten(left);
flatten(right);
}3)后序遍历拼接(变种)
思路:于先序遍历完全相反,由右子树最右节点开始,依次拼接
代码:
TreeNode last;
public void flatten(TreeNode root) {
if (root == null ) return;
flatten(root.right);
flatten(root.left);
//pre表示最后一个节点,依次往上建立
root.right = last;
root.left = null;
last = root;
}总结:这道题万变不离其宗,根据先序遍历的顺序,把左子树拼接到右子树,了解这点就很简单
2、二叉树中的最大路径和
题目描述:

思路:题目是要我们求最大路径和,规定路径的节点只能出现一次

如图路径一般有四种情况
- 我自己就是一条路径
- 只跟左子节点合并成一条路径
- 只跟右子节点合并成一条路径
- 以自己为桥梁,跟左、右子节点合并成一条路径
我们就需要用到递归,求出每一个结点的2、3情况,并返回较大的路径和
同时,在每次递归时,都需要不断进行最大路径和的判断,因为我们不知道在哪个节点时会取到最大路径和!
进入代码:
int max = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
dfs(root);
return max;
}
public int dfs(TreeNode root){
if (root == null) return 0;
//由于路径加起来可能是负的,所以做一个比较
int leftPath = Math.max(0,dfs(root.left));
int rightPath = Math.max(0,dfs(root.right));
max = Math.max(max,root.val + leftPath + rightPath);
//返回左边路径和或者右边路径和的较大值
return root.val + Math.max(leftPath,rightPath);
}持续更新中~~
边栏推荐
- NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】
- MATLAB/Simulink快捷键
- VRRP+MSTP配置详解【华为eNSP实验】
- Libpq 是否支持读写分离配置
- 一道[CSCCTF 2019 Qual]FlaskLight的详解再遇SSTI
- 将jpg图片转换成yuv420(NV12)数据文件
- Post-94 Byte P7 posted the salary slip: It's really good to make up for this...
- Detailed explanation of switch link aggregation [Huawei eNSP]
- [Computer recording screen] How to use bandicam to record the game setting graphic tutorial
- 加降息与BTC流动性事件策略研究
猜你喜欢

加降息与BTC流动性事件策略研究

csdn图片去水印 | 其他方法无效时的解决方案

学会 Arthas,让你 3 年经验掌握 5 年功力

cannot import name ‘import_string‘ from ‘werkzeug‘【bug解决】

Recommend several methods that can directly translate PDF English documents

BFM模型和Landmarks可视化

inject() can only be used inside setup() or functional components.

After four years of outsourcing, the autumn recruits finally landed

Wang Shuang's Assembly Language Chapter 4: The First Program

线程安全问题
随机推荐
优炫数据库只有数据文件如何恢复
【正点原子STM32连载】第一章 本书学习方法 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
【NOI模拟赛】纸老虎博弈(博弈论SG函数,长链剖分)
DOM简述
layout manager
ansible部署脚本--亲测可用无坑
华为od项目
线程的状态
Apache Druid 实时分析数据库入门介绍
JSP基本语法
How to restore the Youxuan database with only data files
字符串与正则表达式(C#)
三层交换机配置MSTP协议详解【华为eNSP实验】
【论文笔记】Understanding Long Programming Languages with Structure-Aware Sparse Attention
注意力机制
Linux Redis cache avalanche, breakdown, penetration
sql在字段重复时 对某个字段根据最新时间取数
DNS 查询原理详解—— 阮一峰的网络日志
蘑菇书EasyRL学习笔记
【论文笔记】Delving into the Estimation Shift of Batch Normalization in a Network