当前位置:网站首页>Merging binary trees by recursion
Merging binary trees by recursion
2022-07-07 08:04:00 【Hydrion-Qlz】
617. Merge binary tree
The difficulty is simple
Here are two binary trees : root1 and root2 .
Imagine , When you cover one tree over the other , Some nodes on the two trees will overlap ( And others don't ). You need to merge these two trees into a new binary tree . The rule of consolidation is : If two nodes overlap , Then add the values of the two nodes as the new values of the merged nodes ; otherwise , Not for null The node of will be the node of the new binary tree directly .
Returns the merged binary tree .
Be careful : The merge process must start at the root of both trees .
Ideas
For trees , Most of the problems can be solved by recursion , This is no exception
For each node , Judge whether the corresponding nodes of the current two trees are null,
- If it's all for null, Then return to null
- If one is for null, Then return to another tree
- If neither is null, Then the return value is the new tree of the sum of two nodes , Then continue to traverse the left and right child nodes of these two nodes
package cn.edu.xjtu.carlWay.tree.mergeTwoBinaryTree;
import cn.edu.xjtu.Util.TreeNode.TreeNode;
/** * 617. Merge binary tree * Here are two binary trees : root1 and root2 . * <p> * Imagine , When you cover one tree over the other , Some nodes on the two trees will overlap ( And others don't ). You need to merge these two trees into a new binary tree . The rule of consolidation is : If two nodes overlap , Then add the values of the two nodes as the new values of the merged nodes ; otherwise , Not for null The node of will be the node of the new binary tree directly . * <p> * Returns the merged binary tree . * <p> * Be careful : The merge process must start at the root of both trees . * <p> * https://leetcode-cn.com/problems/merge-two-binary-trees/ */
public class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
// Both nodes are null
if (root1 == null && root2 == null) {
return null;
}
// One for null
if (root1 == null) {
return root2;
}
if (root2 == null) {
return root1;
}
// Neither is null
TreeNode node = new TreeNode(root1.val + root2.val);
node.left = mergeTrees(root1.left, root2.left);// Continue processing the left child node
node.right = mergeTrees(root1.right, root2.right);// Continue processing the right child node
return node;
}
}
边栏推荐
- These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
- Value sequence (subsequence contribution problem)
- 2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
- Why should we understand the trend of spot gold?
- Linux server development, MySQL cache strategy
- 2022 tea master (intermediate) examination questions and mock examination
- Force buckle 144 Preorder traversal of binary tree
- 2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
- C language communication travel card background system
- Rust versus go (which is my preferred language?)
猜你喜欢

Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!

These five fishing artifacts are too hot! Programmer: I know, delete it quickly!

探索干货篇!Apifox 建设思路
![[unity] several ideas about circular motion of objects](/img/84/e70c6696629dbe17ace011553f43ff.png)
[unity] several ideas about circular motion of objects

php导出百万数据

Linux server development, redis protocol and asynchronous mode

这5个摸鱼神器太火了!程序员:知道了快删!

Numbers that appear only once
![[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)](/img/e1/9a047ef13299b94b5314ee6865ba26.png)
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)

Hands on deep learning (IV) -- convolutional neural network CNN
随机推荐
Quickly use Jacobo code coverage statistics
[unity] several ideas about circular motion of objects
Cnopendata geographical distribution data of religious places in China
Qt学习28 主窗口中的工具栏
The element with setfieldsvalue set is obtained as undefined with GetFieldValue
2022 welder (elementary) judgment questions and online simulation examination
Content of string
[UTCTF2020]file header
Codeforces Global Round 19
Shell 脚本的替换功能实现
Button wizard script learning - about tmall grabbing red envelopes
LeetCode 40:组合总和 II
有 Docker 谁还在自己本地安装 Mysql ?
Numbers that appear only once
[UVM basics] summary of important knowledge points of "UVM practice" (continuous update...)
Implementation of replacement function of shell script
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
Summary of redis functions
Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
CTF daily question day43 rsa5