LeetCode 617. Merge Two Binary Tree (合并两个二叉树)

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.

You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.

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

 

Note: The merging process must start from the root nodes of both trees.

 


题目标题:Tree

 

  这道题目给了我们两个二叉树,要我们合并两个二叉树,合并的树的每一个点的值等于两个二叉树相对位置的点的值的合。利用recursively call来实现,我们来分析一下。对于每一个新的点的值,我们需要做的就是把两个树中的同样位置的点的值相加。然后recursively来继续代入mergeTrees,左边的点,就代入同样位置两个点的左边。右边的点就代入同样位置的两个点的右边,直到代入得两个点都是null,就停止代入,return回去。 那么对于每一个新的点,有三种情况:1- 两个点都是null,就直接return; 2- 两个点都不是null,直接相加;3- 两个点其中有一个点是null,那么就取另外一个点的值。 需要注意的是,对于每一个新的点,如果代入的两个点其中一个是null的话,那么这个null的点的 .left 和.right 是error。所以要先initial 一下。

 

Java Solution:

Runtime beats 60.24% 

完成日期:06/29/2017

关键词:Tree

关键点:利用recursively来求每一个新的点,以及这个点的左右child

 

复制代码
 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution 
11 {
12     public TreeNode mergeTrees(TreeNode t1, TreeNode t2) 
13     {
14         TreeNode root;
15         TreeNode left_1 = null, left_2 = null;
16         TreeNode right_1 = null, right_2 = null;
17         
18         if(t1 == null && t2 == null)
19             return null;
20         else if(t1 != null && t2 != null)
21         {
22             root = new TreeNode(t1.val + t2.val);
23             left_1 = t1.left;
24             left_2 = t2.left;
25             right_1 = t1.right;
26             right_2 = t2.right;
27         }
28         else if(t1 != null && t2 == null)
29         {
30             root = new TreeNode(t1.val);
31             left_1 = t1.left;
32             right_1 = t1.right;
33         }
34         else
35         {
36             root = new TreeNode(t2.val);
37             left_2 = t2.left;
38             right_2 = t2.right;
39         }
40         
41         
42         root.left = mergeTrees(left_1, left_2);
43         root.right = mergeTrees(right_1, right_2);
44         
45             
46         return root;
47     }
48 }
复制代码

参考资料:N/A

 

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

posted @   Jimmy_Cheng  阅读(3457)  评论(0编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
· MQ 如何保证数据一致性?
· 《HelloGitHub》第 108 期
点击右上角即可分享
微信分享提示