当前位置:网站首页>Leetcode 617 merge binary tree
Leetcode 617 merge binary tree
2022-04-23 03:31:00 【Jack_ joker】
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:
Input :
Tree 1 Tree 2
1 2
/ \ / \
3 2 1 3
/ \ \
5 4 7
Output :
Merged tree :
3
/ \
4 5
/ \ \
5 4 7
Merge two binary trees using depth first search . Traverse two binary trees at the same time from the root node , And merge the corresponding nodes .
The corresponding nodes of two binary trees may exist in the following three cases , Use different consolidation methods for each case .
If the corresponding nodes of two binary trees are empty , The corresponding node of the merged binary tree is also empty ;
If only one of the corresponding nodes of two binary trees is empty , Then the corresponding node of the merged binary tree is the non empty node ;
If the corresponding nodes of two binary trees are not empty , Then the value of the corresponding node of the merged binary tree is the sum of the values of the corresponding nodes of the two binary trees , In this case, you need to explicitly merge the two nodes .
After merging a node , Also merge the left and right subtrees of the node respectively .
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if(root1 == null){
return root2;
}
if(root2 == null){
return root1;
}
TreeNode mergeNode = new TreeNode(root1.val + root2.val);
mergeNode.left = mergeTrees(root1.left,root2.left);
mergeNode.right = mergeTrees(root2.right ,root1.right);
return mergeNode;
}
}
版权声明
本文为[Jack_ joker]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220607497443.html
边栏推荐
- Applet - canvas drawing Poster
- Batch download of files ---- compressed and then downloaded
- Application and definition of interface
- Three column layout (fixed width on both sides in the middle and fixed width on both sides in the middle)
- Detailed explanation of socket programming send() and recv() functions
- JS takes out the same elements in two arrays
- Problem C: Hanoi Tower III
- 关于idea调试模式下启动特别慢的优化
- MySql关键字GROUP_CONCAT,组合连接查询
- Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
猜你喜欢
MySql关键字GROUP_CONCAT,组合连接查询
Fiddler use
关于idea调试模式下启动特别慢的优化
QT learning summary
Design and implementation of redis (4): what is the event driver of redis
Database SQL -- simulate inserting a large amount of data, importing / exporting database scripts, timestamp conversion and database basics
淺學一下I/O流和File類文件操作
Deep learning notes (II) -- principle and implementation of activation function
Design and implementation of redis (6): how redis achieves high availability
Database - MySQL -- Navicat import SQL error 1067 - invalid default value for 'paydate‘
随机推荐
QT learning summary
Codeforces round 784 (Div. 4) (AK CF (XD) for the first time)
Applet - canvas drawing Poster
Chapter 8 exception handling, string handling and file operation
你真的懂hashCode和equals吗???
The fourth operation
深度学习笔记(二)——激活函数原理与实现
2021-08-31
Quartz. Www. 18fu Used in net core
Téléchargement en vrac de fichiers - téléchargement après compression
Unity knowledge points (ugui 2)
JS - accuracy issues
Optimization of especially slow startup in idea debugging mode
【微服务】(十)—— 统一网关Gateway
7-3 poly width
Do you really understand hashcode and equals???
The content of the website is prohibited from copying, pasting and saving as JS code
Problem C: realize Joseph Ring with linked list
Super easy to use [general excel import function]
Common exceptions