zoukankan      html  css  js  c++  java
  • LeetCode 617. 合并二叉树(C#实现)——二叉树,递归,迭代

    一、问题

    https://leetcode-cn.com/problems/merge-two-binary-trees/

    给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。
    
    你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。
    
    示例 1:
    
    输入: 
        Tree 1                     Tree 2                  
              1                         2                             
             /                        /                             
            3   2                     1   3                        
           /                                                    
          5                             4   7                  
    输出: 
    合并后的树:
             3
            / 
           4   5
          /     
         5   4   7
    
    注意: 合并必须从两个树的根节点开始。

    二、GitHub实现:https://github.com/JonathanZxxxx/LeetCode/blob/master/MergeClass.cs

      Blog:https://www.cnblogs.com/zxxxx/

    三、思路

      1、递归:对两棵树进行前序遍历,并将对应的节点合并。将当前节点的值进行相加,并对左右孩子进行递归合并,如果为空,就返回另外一棵树作为结果。

      2、迭代:将两棵树的根节点入栈,栈中的每个元素都存放两个根节点,栈顶为当前需要处理的根节点。在每次迭代中,去除栈顶元素并移出栈,并将他们的值相加;如果两个节点都有左孩子,入栈,如果只有一个节点左孩子有值,将其作为第一个节点的左孩子,如果两个节点的左孩子都为空,不做任何处理;右孩子同理。迭代完成后返回第一棵树作为结果。

    四、代码

      1     public class MergeClass
      2     {
      3         /// <summary>
      4         /// 递归
      5         /// </summary>
      6         /// <param name="t1"></param>
      7         /// <param name="t2"></param>
      8         /// <returns></returns>
      9         public TreeNode MergeTrees(TreeNode t1, TreeNode t2)
     10         {
     11             if (t1 == null)
     12             {
     13                 return t2;
     14             }
     15             if (t2 == null)
     16             {
     17                 return t1;
     18             }
     19             t1.val += t2.val;
     20             t1.left = MergeTrees(t1.left, t2.left);
     21             t1.right = MergeTrees(t1.right, t2.right);
     22             return t1;
     23         }
     24 
     25         /// <summary>
     26         /// 栈迭代
     27         /// </summary>
     28         /// <param name="t1"></param>
     29         /// <param name="t2"></param>
     30         /// <returns></returns>
     31         public TreeNode MergeTrees2(TreeNode t1, TreeNode t2)
     32         {
     33             if (t1 == null)
     34             {
     35                 return t2;
     36             }
     37             var stack = new Stack<TreeNode[]>();
     38             stack.Push(new TreeNode[] { t1, t2 });
     39             while (stack.Any())
     40             {
     41                 var pop = stack.Pop();
     42                 if (pop[0] == null || pop[1] == null)
     43                 {
     44                     continue;
     45                 }
     46                 pop[0].val += pop[1].val;
     47                 if (pop[0].left == null)
     48                 {
     49                     pop[0].left = pop[1].left;
     50                 }
     51                 else
     52                 {
     53                     stack.Push(new TreeNode[] { pop[0].left, pop[1].left });
     54                 }
     55                 if (pop[0].right == null)
     56                 {
     57                     pop[0].right = pop[1].right;
     58                 }
     59                 else
     60                 {
     61                     stack.Push(new TreeNode[] { pop[0].right, pop[1].right });
     62                 }
     63             }
     64             return t1;
     65         }
     66 
     67         /// <summary>
     68         /// 队列迭代
     69         /// </summary>
     70         /// <param name="t1"></param>
     71         /// <param name="t2"></param>
     72         /// <returns></returns>
     73         public TreeNode MergeTrees3(TreeNode t1, TreeNode t2)
     74         {
     75             if (t1 == null)
     76             {
     77                 return t2;
     78             }
     79             var queue = new Queue<TreeNode[]>();
     80             queue.Enqueue(new TreeNode[] { t1, t2 });
     81             while (queue.Any())
     82             {
     83                 var current = queue.Dequeue();
     84                 if (current[0] == null || current[1] == null)
     85                 {
     86                     continue;
     87                 }
     88                 current[0].val += current[1].val;
     89                 if (current[0].left == null)
     90                 {
     91                     current[0].left = current[1].left;
     92                 }
     93                 else
     94                 {
     95                     queue.Enqueue(new TreeNode[] { current[0].left, current[1].left });
     96                 }
     97                 if (current[0].right == null)
     98                 {
     99                     current[0].right = current[1].right;
    100                 }
    101                 else
    102                 {
    103                     queue.Enqueue(new TreeNode[] { current[0].right, current[1].right });
    104                 }
    105             }
    106             return t1;
    107         }
    108 
    109         public class TreeNode
    110         {
    111             public int val;
    112             public TreeNode left;
    113             public TreeNode right;
    114             public TreeNode(int x) { val = x; }
    115         }
    116 
    117     }
  • 相关阅读:
    网络连接的小图标无法显示的解决
    ASP马免杀工具—ASP万能溶剂
    网络连接的小图标无法显示的解决
    Metisse:相当酷的窗口经管器
    Realtek ALC268集成声卡驱动问题在ubuntu下的办理方案
    mtPaint:绘制图标及像素图
    Solaris为单块网卡装备多个IP
    无比震荡 Fedora Linux把密码输出都落实起来
    Parted Magic-Linux 中的分区把戏师
    Linux和NTFS冲突
  • 原文地址:https://www.cnblogs.com/zxxxx/p/12132100.html
Copyright © 2011-2022 走看看