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.

Topic title: Tree

  This question gives us two binary trees, and asks us to merge the two binary trees. The value of each point of the merged tree is equal to the sum of the values ​​of the points at the relative positions of the two binary trees. Use recursively call to achieve, let's analyze. For each new point value, all we need to do is add the value of the same point in the two trees. Then recursively continue to substitute mergeTrees, and the left point is substituted into the left side of the two points in the same position. The point on the right is substituted into the right side of the two points in the same position, until both points are null, stop the substitution and return. Then for each new point, there are three cases: 1- If both points are null, return directly; 2- If both points are not null, add them directly; 3- If one of the two points is null, Then take the value of another point. It should be noted that, for each new point, if one of the two substituted points is null, then the .left and .right of the null point are error. So you have to initialize it first.

Java Solution:

Runtime beats 60.24%

Completion date: 06/29/2017

Keywords: Tree

Key point: Use recursively to find each new point, and the left and right children of this point

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution 
{
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) 
    {
        TreeNode root;
        TreeNode left_1 = null, left_2 = null;
        TreeNode right_1 = null, right_2 = null;

        if(t1 == null && t2 == null)
            return null;
        else if(t1 != null && t2 != null)
        {
            root = new TreeNode(t1.val + t2.val);
            left_1 = t1.left;
            left_2 = t2.left;
            right_1 = t1.right;
            right_2 = t2.right;
        }
        else if(t1 != null && t2 == null)
        {
            root = new TreeNode(t1.val);
            left_1 = t1.left;
            right_1 = t1.right;
        }
        else
        {
            root = new TreeNode(t2.val);
            left_2 = t2.left;
            right_2 = t2.right;
        }


        root.left = mergeTrees(left_1, left_2);
        root.right = mergeTrees(right_1, right_2);


        return root;
    }
}

Reference: N/A

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326260796&siteId=291194637