当前位置:网站首页>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
边栏推荐
- Log4net is in Net core usage
- QT learning summary
- Explication détaillée des fonctions send () et recv () du programme Socket
- MySQL installation pit
- PWA I'm here
- Problem a: face recognition
- [microservices] (x) -- Unified gateway
- "Visual programming" test paper
- List interface of collection
- 打卡:4.23 C语言篇 -(1)初识C语言 - (12)结构体
猜你喜欢

2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)

QT learning summary

Node configuration environment CMD does not take effect

MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】

. net webapi access authorization mechanism and process design (header token + redis)

Visual programming - drawing assignment

淺學一下I/O流和File類文件操作

Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800

深度学习笔记(二)——激活函数原理与实现

QT dynamic translation of Chinese and English languages
随机推荐
Deep learning notes (II) -- principle and implementation of activation function
Oracle query foreign keys contain comma separated data
Visual programming - Experiment 1
POI create and export Excel based on data
Chapter 8 exception handling, string handling and file operation
Common exceptions
AWS from entry to actual combat: creating accounts
Unity games and related interview questions
Test questions (2)
C abstract class
Several common methods of multithreading
2022 团体程序设计天梯赛 模拟赛 L1-7 矩阵列平移 (20 分)
MySQL query specifies that a row is sorted to the first row
Un aperçu des flux d'E / s et des opérations de fichiers de classe de fichiers
Cefsharp stores cookies and reads cookies
TCP three handshakes and four waves
Docker拉取mysql并连接
socket編程 send()與 recv()函數詳解
Translation of l1-7 matrix columns in 2022 group programming ladder Simulation Competition (20 points)
Codeforces Round #784 (Div. 4)題解 (第一次AK cf (XD