leetcode 617. Merge Two Binary Trees
来源:互联网 发布:老男孩linux网络班 编辑:程序博客网 时间:2024/06/05 19:24
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 stubMerge_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; }}迭代:当然要用到栈了。
我们使用栈来代替递归,每个栈元素存储 [nodetree1,nodetree2]形式的数据. 在这里,nodetree1 是tree1中的node, 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
- [leetcode]617. Merge Two Binary Trees
- leetcode.617.Merge Two Binary Trees
- [leetcode]: 617. Merge Two Binary Trees
- [Leetcode]617. Merge Two Binary Trees
- LeetCode 617. Merge Two Binary Trees (C++)
- LeetCode 617. Merge Two Binary Trees
- [LeetCode] 617. Merge Two Binary Trees
- LeetCode 617. Merge Two Binary Trees
- 【Leetcode】617. Merge Two Binary Trees
- [LeetCode]617.Merge Two Binary Trees
- LeetCode: 617. Merge Two Binary Trees
- Leetcode 617. Merge Two Binary Trees
- [leetcode]617. Merge Two Binary Trees
- [Leetcode] 617. Merge Two Binary Trees
- [LeetCode]617. Merge Two Binary Trees
- leetcode 617. Merge Two Binary Trees
- 【LeetCode】617. Merge Two Binary Trees
- [LeetCode]617. Merge Two Binary Trees<Java>
- ArrayUtils用法
- 企业级开发的JAVA代码生成器-java快速开发平台源码版
- 收集了50道基础的java面试题
- svn:clean up报错的终极解决方案
- springmvc常用注解标签详解
- leetcode 617. Merge Two Binary Trees
- 沈工工作室开张了
- Linux-搭建DHCP服务器
- 函数式编程初探
- NoSQL Manager for MongoDB 30天到期破解
- CSS3新特性
- 容器CNI完全解读bridge实现(二)
- Adaboost的原理、推导与实例
- Java并发编程之CountDownLatch,CyclicBarrier实现一组线程相互等待、唤醒