当前位置:网站首页>Leetcode brush questions: binary tree 19 (merge binary tree)
Leetcode brush questions: binary tree 19 (merge binary tree)
2022-07-07 12:47:00 【Taotao can't learn English】
617. Merge binary tree
Given two binary trees , Imagine when you overlay one of them on the other , Some nodes of two binary trees will overlap .
You need to merge them into a new binary tree . The rule for merging is if two nodes overlap , Then add their values as the new values after node merging , Otherwise, no NULL The node of will be the node of the new binary tree directly .
Example 1:

Be careful : The merge must start at the root of both trees .
Based on one of the trees , Recursively traverse two trees at the same time , Each time, judge whether the current node exists in the non benchmark tree , Then add , non-existent , Then skip , The current node does not exist , Exist in non benchmark tree , Then add this node with the passed parent node , Remember to mark which tree it is .
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;
}
// Based on the first tree
if (root1 != null && root2 != null) {
root1.val = root1.val + root2.val;
}
if (root1 == null && root2 != null) {
// If 1 No, ,2 Yes
if (isLeft) {
parent.left = root2;
return;
} else {
parent.right = root2;
return;
}
}
// If 1 Yes , 2 No, Do not change
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]")
);
}
}

Let's take a look at the big guy's code
// recursive
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;
}
边栏推荐
- 【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
- Multi row and multi column flex layout
- 2022聚合工艺考试题模拟考试题库及在线模拟考试
- [learn micro services from 0] [02] move from single application to service
- 密码学系列之:在线证书状态协议OCSP详解
- 【从 0 开始学微服务】【03】初探微服务架构
- Airserver automatically receives multi screen projection or cross device projection
- 【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
- The left-hand side of an assignment expression may not be an optional property access. ts(2779)
- Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
猜你喜欢

ICLR 2022 | 基于对抗自注意力机制的预训练语言模型

金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)

【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
![[爬虫]使用selenium时,躲避脚本检测](/img/3a/85ea729be2aa76c3de4a822ca6939b.png)
[爬虫]使用selenium时,躲避脚本检测

基于NeRF的三维内容生成

SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels

The left-hand side of an assignment expression may not be an optional property access. ts(2779)
![[statistical learning method] learning notes - support vector machine (I)](/img/3f/56db88d717d7cd6624b3d0867146e9.png)
[statistical learning method] learning notes - support vector machine (I)

Vxlan 静态集中网关

About IPSec
随机推荐
通讯协议设计与实现
ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer
Day-18 hash table, generic
SQL lab 1~10 summary (subsequent continuous update)
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
[crawler] avoid script detection when using selenium
[learn wechat from 0] [00] Course Overview
Using stack to convert binary to decimal
Airserver automatically receives multi screen projection or cross device projection
【从 0 开始学微服务】【02】从单体应用走向服务化
test
leetcode刷题:二叉树23(二叉搜索树中的众数)
Aike AI frontier promotion (7.7)
File upload vulnerability - upload labs (1~2)
GCC compilation error
Object. Simple implementation of assign()
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
【统计学习方法】学习笔记——第五章:决策树