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

    Easy

    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.

    题目大意:有两棵二叉树,将这两棵树进行合并。合并后的树的各节点的值为输入的两棵树对应节点的值的加和。如果其中一棵树的对应节点为空,则将该节点非空的那个节点值赋给结果。

    对于二叉树的运算我习惯使用递归的方法,写法简单思路清晰:

    先将根节点合并作为新二叉树的根节点,然后将各自的左子树合并变成新二叉树的左子树,右子树合并变成新二叉树的右子树。

    进行左子树和右子树合并时就是将他们都看成是一个单独的树,递归调用合并函数。

    最后注意输入的二叉树可能是空的,所以在开头记得进行判断。

    代码如下:

    class Solution {
    public:
        TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
            if(t1 && !t2)return t1;
            else if(!t1 && t2)return t2;
            else if(!t1 && !t2)return NULL;
            
            TreeNode* res=new TreeNode(t1->val + t2->val);
                    
            if(t1->left && t2->left) res->left=mergeTrees(t1->left,t2->left);
            else{
                if(t1->left && !t2->left) res->left = t1->left;
                else if(!t1->left && t2->left) res->left = t2->left;
            }
            if(t1->right && t2->right) res->right = mergeTrees(t1->right,t2->right);
            else{
                if(t1->right && !t2->right) res->right = t1->right;
                else if(!t1->right && t2->right) res->right = t2->right;
            }
            return res;
        }
    };

    既然在开头有对二叉树是否为空的分类讨论,那么可以将代码简化成下边这样:

    class Solution {
    public:
        TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
            if(t1 && !t2)return t1;
            else if(!t1 && t2)return t2;
            else if(!t1 && !t2)return NULL;
            
            TreeNode* res=new TreeNode(t1->val + t2->val);
                    
            if(t1->left || t2->left) res->left=mergeTrees(t1->left,t2->left);
            if(t1->right || t2->right) res->right = mergeTrees(t1->right,t2->right);
            return res;
        }
    };

    这样写的代码看起来更精简,但是这样的运行时间要较长一些,因为其中一个二叉树的子树为空时仍要进行一次递归调用。

  • 相关阅读:
    Codeforces Round #646 (Div. 2)【B. Subsequence Hate题解】
    关于MyBatis常见映射异常
    SQL语句汇总(终篇)—— 表联接与联接查询【转载自https://www.cnblogs.com/ghost-xyx/p/3813688.html】
    SQL语句汇总(二)——数据修改、数据查询【转载自https://www.cnblogs.com/ghost-xyx/p/3798362.html】
    浮动元素引起的问题和解决办法
    PHP 神奇的sprintf函数
    关于this,作用域,属性,原型链的一个小练习
    for...of 与 for...in 区别
    ES6 Promise对象then方法链式调用
    ES6通过WeakMap解决内存泄漏问题
  • 原文地址:https://www.cnblogs.com/cff2121/p/11430426.html
Copyright © 2011-2022 走看看