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

    LeetCode Notes_#617 Merge Two Binary Trees

    Contents

    题目

    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

    解答

    方法1:递归法

    错误写法

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

    分析:在前两个条件判断里边,我只是给root赋值,是不对的。没有考虑到t1和t2同时为null的情况,t1,t2同时为null,则进入了最后一种情况,这时候肯定报NullPointerException。

    修改如下

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

    还是不对,因为这样改的话,当t1和t2同时为null的时候,root还是会等于null.

    正解

    class Solution {
        public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
            TreeNode root;
            if (t1 == null) return t2;
            if (t2 == null) return t1;
            root = new TreeNode(t1.val + t2.val);
            root.left = mergeTrees(t1.left,t2.left);
            root.right = mergeTrees(t1.right,t2.right);
            return root;
        }
    }
    • t1或t2任何一个为null,直接返回另一个,意味着接下来的其他代码就不再继续执行了。
    • 递归函数里边,前边两个条件的return并不是返回最终结果,而是返回一个中间结果。结束的条件是返回的中间结果为null。
    • 最后的return root返回的则是真正的最终结果。每一轮迭代其实就是在向下遍历两棵树,终止条件是遇到其中一棵树的节点为null。
    • 感觉递归也可以看做是一种特殊的循环,需要调用一个函数多次,但是什么时候该结束呢,一定需要有一个return的语句去终止递归调用,这样的return后面跟着的一定是一个可以确定的值或者对象。而函数的末尾的return一般都是另一种类型,也就是一个“对自身的调用”,这是无法立即得到结果的,会一直循环的调用很多层,直到遇到开头的有确定值的return,递归调用就开始一层层的返回。

    方法2:迭代法

    有点复杂,暂时搁置,将来再看。

  • 相关阅读:
    .NET设计模式系列文章
    [转]给年轻工程师的十大忠告
    [你必须知道的.NET]第二十回:学习方法论
    写给开发者看的关系型数据库设计
    AjaxPro使用说明
    Spring.Net入门篇(一) [转]
    [从设计到架构] 必须知道的设计模式
    4月1日SharePoint Designer将开始免费
    12月累计更新的一个导出导入网站的问题在2月累计更新中修复了
    修复错误1093 “Unable to get the private bytes memory limit for the W3WP process”
  • 原文地址:https://www.cnblogs.com/Howfars/p/12402830.html
Copyright © 2011-2022 走看看