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;
        }
    };

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

  • 相关阅读:
    PHP pcntl
    Linux 远程登录命令telnet
    git .gitignore不生效
    使用 GoLand 启动 运行 Go 项目
    Go语言: 万物皆异步
    MYSQL 单表一对多查询,将多条记录合并成一条记录
    详解PHP中instanceof关键字及instanceof关键字有什么作用
    all_user_func()详解
    python的反射
    python 的魔术方法
  • 原文地址:https://www.cnblogs.com/cff2121/p/11430426.html
Copyright © 2011-2022 走看看