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

    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.

    思路:层次遍历,往队列里每次塞进去两棵树相同位置上不为空的节点,因为最终输出的是左树,则节点一共有以下3种情况:

    1.左右树对应位置上孩子只有左树为空,则把右树孩子赋给左树对应位置

    2.左右树对应位置上孩子都不为空,直接塞进队列里就可以了,出队列时累加起来给左树

    3.左右树对应位置上孩子只有右树为空,则不需要处理,我们需要输出的是左边的树(X)

     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution 
    11 {
    12 public:
    13     TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) 
    14     {
    15         if(t1 == nullptr) return t2;
    16         if(t2 == nullptr) return t1;
    17         if(t1 == nullptr && t2 == nullptr) return nullptr;
    18         else
    19         {
    20           queue<TreeNode*>q;
    21           q.push(t1);
    22           q.push(t2);
    23           while(!q.empty())
    24           {
    25             TreeNode* l1 = q.front();
    26             q.pop();
    27             TreeNode* l2 = q.front();
    28             q.pop();
    29             l1->val +=l2->val;
    30             if(l1->left != nullptr && l2->left!=nullptr)
    31             {
    32               q.push(l1->left);
    33               q.push(l2->left);
    34             }
    35             if(l1->right != nullptr && l2->right!=nullptr)
    36             {
    37               q.push(l1->right);
    38               q.push(l2->right);
    39             }
    40             if(l1->left == nullptr && l2->left != nullptr )
    41             {
    42               l1->left = l2->left;
    43             }
    44             if(l1->right == nullptr && l2->right != nullptr)
    45             {
    46               l1->right = l2->right;
    47             }
    48           }
    49           return t1;
    50         }
    51     }
    52 };
  • 相关阅读:
    97. 交错字符串-7月18日
    如何判断一个区块链项目的好坏?
    不知道这10点,千万别用SaaS
    数字人民币应用的五大猜想!你最关心哪个?
    什么是人工智能核心?这2个功能上线
    大数据的七大核心具体价值
    机器学习操作正在兴起
    每个大数据架构师都需要的6个基本技能
    数据之美:可视化会给你意想不到的答案!
    如何采用人工智能创建自动化运营的数据中心
  • 原文地址:https://www.cnblogs.com/Swetchine/p/11247895.html
Copyright © 2011-2022 走看看