当前位置:网站首页>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;
}
边栏推荐
- 密码学系列之:在线证书状态协议OCSP详解
- [statistical learning methods] learning notes - Chapter 4: naive Bayesian method
- Realize all, race, allsettled and any of the simple version of promise by yourself
- leetcode刷题:二叉树20(二叉搜索树中的搜索)
- 【统计学习方法】学习笔记——支持向量机(上)
- 广州市召开安全生产工作会议
- How much does it cost to develop a small program mall?
- Niuke website
- [statistical learning method] learning notes - support vector machine (I)
- Talk about four cluster schemes of redis cache, and their advantages and disadvantages
猜你喜欢
![[statistical learning method] learning notes - logistic regression and maximum entropy model](/img/f7/857d053cc2cee81c24919aafab3c6e.png)
[statistical learning method] learning notes - logistic regression and maximum entropy model

2022广东省安全员A证第三批(主要负责人)考试练习题及模拟考试

What is an esp/msr partition and how to create an esp/msr partition

普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备

SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
![[deep learning] image multi label classification task, Baidu paddleclas](/img/dd/6f213a396e8bb240a6872e4c03afab.png)
[deep learning] image multi label classification task, Baidu paddleclas

idm服务器响应显示您没有权限下载解决教程

2022危险化学品生产单位安全生产管理人员考题及在线模拟考试

SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)

Vxlan 静态集中网关
随机推荐
Common knowledge of one-dimensional array and two-dimensional array
Cryptography series: detailed explanation of online certificate status protocol OCSP
Customize the web service configuration file
gcc 编译报错
Sorting, dichotomy
通讯协议设计与实现
【统计学习方法】学习笔记——第五章:决策树
Day-19 IO stream
leetcode刷题:二叉树24(二叉树的最近公共祖先)
xshell评估期已过怎么办
The left-hand side of an assignment expression may not be an optional property access. ts(2779)
Polymorphism, final, etc
leetcode刷题:二叉树27(删除二叉搜索树中的节点)
[疑难杂症]pip运行突然出现ModuleNotFoundError: No module named ‘pip‘
[deep learning] image multi label classification task, Baidu paddleclas
图像像素读写操作
Importance of database security
编译 libssl 报错
leetcode刷题:二叉树21(验证二叉搜索树)
leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)