zoukankan      html  css  js  c++  java
  • LeetCode 617. Merge Two Binary Trees

    原题链接在这里:https://leetcode.com/problems/merge-two-binary-trees/

    题目:

    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.

    题解:

    Recursion 解法从上到下.

    一个为null就返回另一个, 否则把sum归到t1上,再更新t1的left and right child.

    Time Complexity: O(n). n 是2个tree中node较多的node数目

    Space: O(logn).

    AC Java:

     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     public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
    12         if(t1 == null){
    13             return t2;
    14         }
    15         if(t2 == null){
    16             return t1;
    17         }
    18         
    19         t1.val += t2.val;
    20         t1.left = mergeTrees(t1.left, t2.left);
    21         t1.right = mergeTrees(t1.right, t2.right);
    22         
    23         return t1;
    24     }
    25 }

    Iteration 解法用stack来替换recursion. stack里存放t1 与 t2组成的array.

    试着归值到t1上. 如果t1 的child为null就直接把t2对应的child赋值到t1上.

    t1的child不为 null 时就把t1 与 t2对应的child放进 stack.

    所以 stack 中 t1 的位置不可能再是 null, 但t2有可能是 null.

    Time Complexity: O(n).

    Space: O(logn), stack space.

    AC Java:

     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     public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
    12         if(t1 == null){
    13             return t2;
    14         }
    15         Stack<TreeNode []> stk = new Stack<TreeNode []>();
    16         stk.push(new TreeNode[] {t1, t2});
    17         while(!stk.isEmpty()){
    18             TreeNode [] cur = stk.pop();
    19             if(cur[1] == null){
    20                 continue;
    21             }
    22             cur[0].val += cur[1].val;
    23             if(cur[0].left == null){
    24                 cur[0].left = cur[1].left;
    25             }else{
    26                 stk.push(new TreeNode[] {cur[0].left, cur[1].left});
    27             }
    28             
    29             if(cur[0].right == null){
    30                 cur[0].right = cur[1].right;
    31             }else{
    32                 stk.push(new TreeNode[] {cur[0].right, cur[1].right});
    33             }
    34         }
    35         return t1;
    36     }
    37 }
  • 相关阅读:
    Winform中用了皮肤控件之后,报错:容量超出了最大容量 参数名:capacity
    C# 生成二维码
    T-sql语句修改数据库逻辑名、数据库名、物理名
    ASP.NET MVC中使用jQuery时的浏览器缓存问题
    关于asp.net页面缓存
    关于VS 工具箱灰色,不可用的解决方案
    Android
    如何让一个DIV水平,垂直方向都居中于浏览器?
    cookie.setPath()的用法
    CSS选择器
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/7381181.html
Copyright © 2011-2022 走看看