当前位置:网站首页>leetcode刷题:二叉树19(合并二叉树)
leetcode刷题:二叉树19(合并二叉树)
2022-07-07 10:30:00 【涛涛英语学不进去】
617.合并二叉树
给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。
你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。
示例 1:
注意: 合并必须从两个树的根节点开始。
以其中一棵树为基准,递归同时遍历两棵树,每次判断当前结点在非基准树里若存在,则相加,不存在,则跳过,当前结点不存在,在非基准树里存在,则用传来的父结点添加这个结点,记得标记一下是哪棵树。
package com.programmercarl.tree;
import com.programmercarl.util.GenerateTreeNode;
import java.util.ArrayDeque;
import java.util.Deque;
/** * @ClassName MergeTrees * @Descriotion TODO * @Author nitaotao * @Date 2022/7/5 18:02 * @Version 1.0 **/
public class MergeTrees {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 != null) {
return root2;
}
if (root1 != null && root2 == null) {
return root1;
}
if (root1 == null && root2 == null) {
return null;
}
merge(root1, root2, false,null);
return root1;
}
public void merge(TreeNode root1, TreeNode root2, boolean isLeft,TreeNode parent) {
if (root1 == null && root2 == null) {
return;
}
// 以第一棵树为基准
if (root1 != null && root2 != null) {
root1.val = root1.val + root2.val;
}
if (root1 == null && root2 != null) {
//如果 1 没有,2 有
if (isLeft) {
parent.left = root2;
return;
} else {
parent.right = root2;
return;
}
}
//如果 1 有 , 2 没有 不做改变
if (root1 != null && root2 == null) {
return;
}
merge(root1.left, root2.left, true,root1);
merge(root1.right, root2.right, false,root1);
}
public static void main(String[] args) {
new MergeTrees().mergeTrees(
GenerateTreeNode.generateTreeNode("[9,-1,null,-2,0,-4,null,null,8,-5,-3,6,null,null,null,null,null,null,7]")
,
GenerateTreeNode.generateTreeNode("[-1,-2,0,null,null,null,8,6,null,null,7]")
);
}
}
来看看大佬的代码
// 递归
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if (root1 == null) return root2;
if (root2 == null) return root1;
TreeNode newRoot = new TreeNode(root1.val + root2.val);
newRoot.left = mergeTrees(root1.left,root2.left);
newRoot.right = mergeTrees(root1.right,root2.right);
return newRoot;
}
边栏推荐
- How to understand the clothing industry chain and supply chain
- Vxlan 静态集中网关
- College entrance examination composition, high-frequency mention of science and Technology
- 小红书微服务框架及治理等云原生业务架构演进案例
- Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
- What are the technical differences in source code anti disclosure
- DOM parsing XML error: content is not allowed in Prolog
- gcc 编译报错
- Cenos openssh upgrade to version 8.4
- 【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
猜你喜欢
对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
Epp+dis learning road (2) -- blink! twinkle!
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
idm服务器响应显示您没有权限下载解决教程
SQL Lab (41~45) (continuous update later)
Preorder, inorder and postorder traversal of binary tree
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
2022 8th "certification Cup" China University risk management and control ability challenge
The left-hand side of an assignment expression may not be an optional property access. ts(2779)
How to use PS link layer and shortcut keys, and how to do PS layer link
随机推荐
Hi3516 full system type burning tutorial
【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
牛客网刷题网址
Solutions to cross domain problems
Will the filing free server affect the ranking and weight of the website?
【PyTorch实战】图像描述——让神经网络看图讲故事
数据库系统原理与应用教程(011)—— 关系数据库
Configure an encrypted web server
盘点JS判断空对象的几大方法
Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool
【统计学习方法】学习笔记——支持向量机(下)
Multi row and multi column flex layout
【PyTorch实战】用RNN写诗
【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
解密GD32 MCU产品家族,开发板该怎么选?
VSCode的学习使用
idm服务器响应显示您没有权限下载解决教程
Upgrade from a tool to a solution, and the new site with praise points to new value
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels