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:迭代法

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

  • 相关阅读:
    B1031
    B1021
    B1021
    B1021
    Android 系统服务一览表
    MULTI-INTERFACE CONNECTIVITY ON ANDROID
    ConnectivityManager 确定和监控网络连接状态
    Android系统各种类型的service刨根解读
    Android Netd
    Android am 指令的使用
  • 原文地址:https://www.cnblogs.com/Howfars/p/12402830.html
Copyright © 2011-2022 走看看