zoukankan      html  css  js  c++  java
  • [LeetCode] 617. Merge Two Binary Trees(合并两棵二叉树)

    Description

    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.

    你需要把这两棵二叉树合并为一棵新的二叉树。合并规则是:如果两个节点互相覆盖,取这两个节点值的和作为新节点的值;否则,取非空的节点的值作为新节点的值。

    Examples

    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.

    合并过程都是从两节点的根开始的。

    Solution

    此题的解题思路来自于题目中对于合并规则的描述。按照合并规则的描述,可以很容易地写出合并根节点的代码,然后再通过对左右子树递归调用方法,来实现对左右子树的合并,最后完成此题。

    /**
     * Example:
     * var ti = TreeNode(5)
     * var v = ti.`val`
     * Definition for a binary tree node.
     * class TreeNode(var `val`: Int) {
     *     var left: TreeNode? = null
     *     var right: TreeNode? = null
     * }
     */
    class Solution {
        fun mergeTrees(t1: TreeNode?, t2: TreeNode?): TreeNode? {
            // 对空树的处理:一个为空就返回另一个
            if (t1 == null) {
                return t2
            }
            if (t2 == null) {
                return t1
            }
            // 两个均为非空,则先将树根的值进行合并
            val newNode = TreeNode(t1.`val` + t2.`val`)
            // 然后依次对左子树和右子树以相同的方式处理
            newNode.left = mergeTrees(t1.left, t2.left)
            newNode.right = mergeTrees(t1.right, t2.right)
            return newNode
        }
    }
    

    上面的代码虽然能够解决问题,但就我个人认为,这个解法不应该对原先传入的树造成影响。上面的代码会存在一种情况:新的节点的 leftright 指针可能会指向原树中的某一个节点。利用 Kotlin 的语法特性,可以得到本题的另一种写法,虽然未经测试,但目测来看应该解决了这一问题:

    /**
     * Example:
     * var ti = TreeNode(5)
     * var v = ti.`val`
     * Definition for a binary tree node.
     * class TreeNode(var `val`: Int) {
     *     var left: TreeNode? = null
     *     var right: TreeNode? = null
     * }
     */
    class Solution {
        fun mergeTrees(t1: TreeNode?, t2: TreeNode?): TreeNode? {
            // 对两棵树均为空树的特判
            if (t1 == null && t2 == null) {
                return null
            }
            // 思路与刚才相同,但利用 Kotlin 的语法糖少写一些 if 判断
            val lVal = t1?.`val`?:0
            val rVal = t2?.`val`?:0
            val newRoot = TreeNode(lVal + rVal)
            newRoot.left = mergeTrees(t1?.left, t2?.left)
            newRoot.right = mergeTrees(t1?.right, t2?.right)
            return newRoot
        }
    }
    
  • 相关阅读:
    GBPR: Group Preference Based Bayesian Personalized Ranking for One-Class Collaborative Filtering-IJACA 2013_20160421
    BPR: Bayesian Personalized Ranking from Implicit Feedback-CoRR 2012——20160421
    基于矩阵分解的推荐算法
    svmlight使用说明
    论文笔记Outline
    Libliner 中的-s 参数选择:primal 和dual
    查询日志方法
    集合 Vector ArrayList 集合一
    c语言 常用知识点
    简单的c语言小程序 回光返照
  • 原文地址:https://www.cnblogs.com/zhongju/p/13770174.html
Copyright © 2011-2022 走看看