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

617.合并二叉树-day18

合并二叉树,也是二叉树操作的经典题目,如果没有接触过的话,其实并不简单,因为我们习惯了操作一个二叉树,一起操作两个二叉树,还会有点懵懵的。

迭代法中,一般一起操作两个树都是使用队列模拟类似层序遍历,同时处理两个树的节点,这种方式最好理解,如果用模拟递归的思路的话,要复杂一些。

最后拓展中,我给了一个操作指针的野路子,大家随便看看就行了,如果学习C++的话,可以在去研究研究。

`class Solution {
// 递归
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if (root1 == null) return root2;
if (root2 == null) return root1;

    root1.val += root2.val;root1.left = mergeTrees(root1.left,root2.left);root1.right = mergeTrees(root1.right,root2.right);return root1;
}

}

class Solution {
// 使用栈迭代
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if (root1 == null) {
return root2;
}
if (root2 == null) {
return root1;
}
Stack stack = new Stack<>();
stack.push(root2);
stack.push(root1);
while (!stack.isEmpty()) {
TreeNode node1 = stack.pop();
TreeNode node2 = stack.pop();
node1.val += node2.val;
if (node2.right != null && node1.right != null) {
stack.push(node2.right);
stack.push(node1.right);
} else {
if (node1.right == null) {
node1.right = node2.right;
}
}
if (node2.left != null && node1.left != null) {
stack.push(node2.left);
stack.push(node1.left);
} else {
if (node1.left == null) {
node1.left = node2.left;
}
}
}
return root1;
}
}

class Solution {
// 使用队列迭代
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if (root1 == null) return root2;
if (root2 ==null) return root1;
Queue queue = new LinkedList<>();
queue.offer(root1);
queue.offer(root2);
while (!queue.isEmpty()) {
TreeNode node1 = queue.poll();
TreeNode node2 = queue.poll();
// 此时两个节点一定不为空,val相加
node1.val = node1.val + node2.val;
// 如果两棵树左节点都不为空,加入队列
if (node1.left != null && node2.left != null) {
queue.offer(node1.left);
queue.offer(node2.left);
}
// 如果两棵树右节点都不为空,加入队列
if (node1.right != null && node2.right != null) {
queue.offer(node1.right);
queue.offer(node2.right);
}
// 若node1的左节点为空,直接赋值
if (node1.left == null && node2.left != null) {
node1.left = node2.left;
}
// 若node2的左节点为空,直接赋值
if (node1.right == null && node2.right != null) {
node1.right = node2.right;
}
}
return root1;
}
}
`

http://www.jsqmd.com/news/482135/

相关文章:

  • 使用GIMP去除水印的有效方法
  • 洛谷题单指南-基础线性代数-P2151 [SDOI2009] HH 去散步
  • 洛洛电竞三角洲代肝(招人)
  • 为什么很多医院(尤其中医院)卖药 —— 院内挂网、院外卖药
  • go 语言之map
  • Pipelined-SAR ADC全流程设计:从理论到实践
  • 20260314 模拟测 总结
  • 1022: 淘金
  • ICPC2025四川省赛题解
  • 701. 二叉搜索树中的插入操作-day18
  • java6
  • 1023: 巨人排队
  • 探秘2026荧光粉领域:口碑佳的企业都有谁,可靠的荧光粉哪家好精选实力品牌 - 品牌推荐师
  • L2-024 部落(简单的并查集)
  • 振动料斗怎么选?2026年口碑厂家大揭秘,振动料斗哪家好精选优质品牌解析 - 品牌推荐师
  • Windows系统木马病毒排查与防治方案
  • deepseek的人性化
  • 最近在研究一个基于三菱PLC和组态王的物流货物分拣控制系统,感觉挺有意思的,分享一下我的思路和代码实现
  • 分辨率与WLAN
  • 【卫星】GNSS多路径效应分析【含Matlab源码 15170期】
  • 【电池】LPV模型预测控制方法和耦合电热模型的电池状态估计【含Matlab源码 15171期】
  • VitaBench: Benchmarking LLM Agents with Versatile Interactive Tasks in Real-world Applications
  • 【电池】PMP算法的插电式混合动力车能量优化控制策略【含Matlab源码 15172期】
  • CSDN技术盲盒挑战全攻略
  • 【电磁】计算电阻率层析成像(ERT)表面和跨井(XBH)电极配置的2D和3D灵敏度分布【含Matlab源码 15173期】
  • 【电力系统】风电、光伏与储能(含电池和废弃矿井小型抽水蓄能)互补调度运行研究【含Matlab源码 15174期】
  • 软考高项-成本管理
  • 基于深度学习的工程车辆检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
  • js之xml处理
  • 【卫星】基于matlab GNSS多路径效应分析【含Matlab源码 15170期】