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 };
  • 相关阅读:
    创业失败的七个原因及解决之道
    技术人员如何参与产品设计讨论:激活那一潭死水
    基于Android Studio搭建hello world工程
    WINCE6.0+IMX515通过cfimager.exe烧录镜像文件
    基于Android Studio搭建Android应用开发环境
    WinCE启动失败的原因与解决办法分析
    Maximum Allowed Error 7 错误解决
    s3c6410 开发板Linux系统支持 K9GAG08U0E的方法
    Nandflash 驱动移植
    GIFT-EMS礼记----青软S2SH(笔记)
  • 原文地址:https://www.cnblogs.com/Swetchine/p/11247895.html
Copyright © 2011-2022 走看看