Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LeetCode 617. Merge Two Binary Trees #36

Open
Woodyiiiiiii opened this issue Apr 28, 2020 · 0 comments
Open

LeetCode 617. Merge Two Binary Trees #36

Woodyiiiiiii opened this issue Apr 28, 2020 · 0 comments

Comments

@Woodyiiiiiii
Copy link
Owner

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.


类似先序遍历创建二叉树那样。

class Solution {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        if (t1 == null || t2 == null)
            return t1 == null ? t2 : t1;
        TreeNode root = new TreeNode(t1.val + t2.val);
        root = create(root, t1, t2);
        return root;
    }
    public TreeNode create(TreeNode node, TreeNode t1, TreeNode t2) {
        if (t1 == null && t2 == null)
            return null;
        else if (t1 != null && t2 == null) {
            node = new TreeNode(t1.val);
            node.left = create(node.left, t1.left, null);
            node.right = create(node.right, t1.right, null);
            return node;
        }
        else if (t1 == null && t2 != null) {
            node = new TreeNode(t2.val);
            node.left = create(node.left, null, t2.left);
            node.right = create(node.right, null, t2.right);
            return node;
        }
        else {
            node = new TreeNode(t1.val + t2.val);
            node.left = create(node.left, t1.left, t2.left);
            node.right = create(node.right, t1.right, t2.right);
            return node;
        }
    }
}

大佬还有更简便的做法,不用编写额外的函数:

class Solution {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        if (t1 == null || t2 == null)
            return t1 == null ? t2 : t1;
        TreeNode root = new TreeNode(t1.val + t2.val);
        root.left = mergeTrees(t1.left, t2.left);
        root.right = mergeTrees(t1.right, t2.right);
        return root;
    }
}

本质上都是对递归二叉树的理解。


参考资料:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant