leetcode 617. Merge Two Binary Trees

257 篇文章 17 订阅

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.

发了个烧感觉脑子就不太好了。如果root.left=null时,进行DFS(root.left , t1,left , t2.left)  然后在深入到root=root.left的DFS方法中进行root=new TreeNode(2);这样的赋值时,最终结果还是root.left=null,即进行函数调用后,对传入函数的root.left进行赋值,无论是赋值new TreeNode还是赋值null,都没有丝毫卵用,不会改变root.left的结果。只能直接对root.left进行赋值,而不能将其传入函数再在函数中对其赋值。我搞得特别复杂。

package leetcode;

public class Merge_Two_Binary_Trees_617 {

	public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
		if(t1==null){
			t1=t2;
			return t1;
		}
		else if(t2==null){
			return t1;
		}
		TreeNode root=new TreeNode(0);
		DFS(null,false,root,t1,t2);
		return root;
	}
	
	public void DFS(TreeNode before,boolean isLeft,TreeNode root,TreeNode t1, TreeNode t2){
		if(t1!=null&&t2!=null){
			root.val=t1.val+t2.val;
			root.left=new TreeNode(0);
			root.right=new TreeNode(0);
			DFS(root,true,root.left,t1.left,t2.left);
			DFS(root,false,root.right,t1.right,t2.right);
		}
		else if(t1!=null&&t2==null){
			root.val=t1.val;
			root.left=new TreeNode(0);
			root.right=new TreeNode(0);
			DFS(root,true,root.left,t1.left,null);
			DFS(root,false,root.right,t1.right,null);
		}
		else if(t1==null&&t2!=null){
			root.val=t2.val;
			root.left=new TreeNode(0);
			root.right=new TreeNode(0);
			DFS(root,true,root.left,null,t2.left);
			DFS(root,false,root.right,null,t2.right);
		}
		else{
			if(isLeft==true){
				before.left=null;
			}
			else{
				before.right=null;
			}		
			return;
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Merge_Two_Binary_Trees_617 m=new Merge_Two_Binary_Trees_617();
		TreeNode root1=new TreeNode(1);
		root1.left=new TreeNode(3);
		root1.right=new TreeNode(2);
		root1.left.left=new TreeNode(5);
		TreeNode root2=new TreeNode(2);
		root2.left=new TreeNode(1);
		root2.right=new TreeNode(3);
		root2.left.right=new TreeNode(4);
		root2.right.right=new TreeNode(7);
		TreeNode t=m.mergeTrees(root1, root2);
		System.out.println(1);
	}

}
大神解法则很简洁,用了递归和迭代。

递归:

public class Solution {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        if (t1 == null)
            return t2;
        if (t2 == null)
            return t1;
        t1.val += t2.val;
        t1.left = mergeTrees(t1.left, t2.left);
        t1.right = mergeTrees(t1.right, t2.right);
        return t1;
    }
}
迭代:当然要用到栈了。

我们使用栈来代替递归,每个栈元素存储 [node_{tree1}, node_{tree2}][nodetree1,nodetree2]形式的数据. 在这里,node_{tree1}nodetree1 是tree1中的node, node_{tree2}nodetree2 是tree2中的node.

可以见https://leetcode.com/problems/merge-two-binary-trees/#/solution中的动图。

public class Solution {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        if (t1 == null)
            return t2;
        Stack < TreeNode[] > stack = new Stack < > ();
        stack.push(new TreeNode[] {t1, t2});
        while (!stack.isEmpty()) {
            TreeNode[] t = stack.pop();
            if (t[0] == null || t[1] == null) {
                continue;
            }
            t[0].val += t[1].val;
            if (t[0].left == null) {
                t[0].left = t[1].left;
            } else {
                stack.push(new TreeNode[] {t[0].left, t[1].left});
            }
            if (t[0].right == null) {
                t[0].right = t[1].right;
            } else {
                stack.push(new TreeNode[] {t[0].right, t[1].right});
            }
        }
        return t1;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值