当前位置:网站首页>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;
}
}
边栏推荐
- 运放电路的反馈电阻上并联一个电容是什么作用
- Shell 脚本的替换功能实现
- 解决问题:Unable to connect to Redis
- SQL优化的魅力!从 30248s 到 0.001s
- 贝叶斯定律
- Wechat applet data binding multiple data
- 青龙面板-今日头条
- Introduction to basic components of wechat applet
- [UVM practice] Chapter 1: configuring the UVM environment (taking VCs as an example), run through the examples in the book
- Linux server development, MySQL index principle and optimization
猜你喜欢

Padavan manually installs PHP

Ansible

探索干货篇!Apifox 建设思路
![[experience sharing] how to expand the cloud service icon for Visio](/img/42/dba9f78f3fb2049dad8b343b0b36e5.png)
[experience sharing] how to expand the cloud service icon for Visio

Use and analysis of dot function in numpy

【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门

json 数据展平pd.json_normalize

【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)

Codeforce c.strange test and acwing

Thinkcmf6.0 installation tutorial
随机推荐
What is the interval in gatk4??
json 数据展平pd.json_normalize
2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
这5个摸鱼神器太火了!程序员:知道了快删!
Codeforces Global Round 19
[UTCTF2020]file header
Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
LeetCode 90:子集 II
LeetCode简单题之判断一个数的数字计数是否等于数位的值
大视频文件的缓冲播放原理以及实现
Bugku CTF daily one question chessboard with only black chess
Thinkcmf6.0 installation tutorial
Thinkcmf6.0安装教程
Binary tree and heap building in C language
【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
3D reconstruction - stereo correction
【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
dash plotly
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers