LeetCode 617. The combined binary tree (C # implementation) - binary tree, recursion, iteration

First, the problem

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

Given two binary tree, when you imagine them to one another on the cover, some nodes will overlap two binary tree. 

You will need to merge them into a new binary tree. Consolidation of the rule is that if two nodes overlap, their values are added as a new value after the merger node, or the node is not NULL will direct the new node as a binary tree. 

Example 1 : 

Input: 
    Tree 1                      Tree 2                   
          1                          2                              
         / \ / \                            
         3    2                      1    3                         
       /                            \ \                      
       . 5                              . 4    . 7                   
outputs: 
the merged tree: 
         3 
        / \
        . 4    . 5 
      / \ \ 
     5    4    7 

Note: The merger must start from the root two trees.

Two, GitHub achieve: https://github.com/JonathanZxxxx/LeetCode/blob/master/MergeClass.cs

Third, the idea

  1, the recursive: for the former two tree traversal order, and merging the corresponding node. The value of the current node are summed and about children recursively merger, if it is empty, it returns another as a result of a tree.

  2, iterations: the two trees root stack, the stack of each element are stored two root, root for the current top of the stack need to be addressed. In each iteration, removing the top element and out of the stack and adds to their value; if two nodes have left children, stack, if there is only one node left child has value, as the first node the left child, if the child left two nodes are empty, without any treatment; the right child empathy. After the first iteration is complete tree returned as the result.

Fourth, the code

  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     }

 

Guess you like

Origin www.cnblogs.com/zxxxx/p/12132100.html