当前位置:网站首页>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;
}
}
边栏推荐
- 2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
- Leetcode 90: subset II
- 【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)
- dash plotly
- Roulette chart 2 - writing of roulette chart code
- Visualization Document Feb 12 16:42
- Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
- Linux server development, MySQL transaction principle analysis
- Linux Installation MySQL 8.0 configuration
- Cnopendata American Golden Globe Award winning data
猜你喜欢
Quickly use Jacobo code coverage statistics
Hands on deep learning (IV) -- convolutional neural network CNN
Codeforce c.strange test and acwing
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
即刻报名|飞桨黑客马拉松第三期等你挑战
Ansible
快速使用 Jacoco 代码覆盖率统计
Cnopendata list data of Chinese colleges and Universities
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
Thinkcmf6.0安装教程
随机推荐
Rust versus go (which is my preferred language?)
Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
Pytest+allure+jenkins environment -- completion of pit filling
Linux Installation MySQL 8.0 configuration
即刻报名|飞桨黑客马拉松第三期等你挑战
力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
[mathematical notes] radian
padavan手动安装php
Explore dry goods! Apifox construction ideas
QT learning 28 toolbar in the main window
pytest+allure+jenkins安装问题:pytest: error: unrecognized arguments: --alluredir
The charm of SQL optimization! From 30248s to 0.001s
Hands on deep learning (IV) -- convolutional neural network CNN
【VHDL 并行语句执行】
探索干货篇!Apifox 建设思路
[VHDL parallel statement execution]
Roulette chart 2 - writing of roulette chart code
[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
Chip information website Yite Chuangxin