| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | using LeetCode.Core.Models; |
| | 13 | |
|
| | 14 | | namespace LeetCode.Algorithms.MergeTwoBinaryTrees; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class MergeTwoBinaryTreesBreadthFirstSearch : IMergeTwoBinaryTrees |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(n + m) |
| | 21 | | /// Space complexity - O(n + m) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="root1"></param> |
| | 24 | | /// <param name="root2"></param> |
| | 25 | | /// <returns></returns> |
| | 26 | | public TreeNode? MergeTrees(TreeNode? root1, TreeNode? root2) |
| 5 | 27 | | { |
| 5 | 28 | | if (root1 == null && root2 == null) |
| 1 | 29 | | { |
| 1 | 30 | | return null; |
| | 31 | | } |
| | 32 | |
|
| 4 | 33 | | if (root1 == null) |
| 1 | 34 | | { |
| 1 | 35 | | return root2; |
| | 36 | | } |
| | 37 | |
|
| 3 | 38 | | if (root2 == null) |
| 1 | 39 | | { |
| 1 | 40 | | return root1; |
| | 41 | | } |
| | 42 | |
|
| 2 | 43 | | var root1Queue = new Queue<TreeNode>(); |
| 2 | 44 | | var root2Queue = new Queue<TreeNode>(); |
| | 45 | |
|
| 2 | 46 | | root1Queue.Enqueue(root1); |
| 2 | 47 | | root2Queue.Enqueue(root2); |
| | 48 | |
|
| 6 | 49 | | while (root1Queue.Count > 0 || root2Queue.Count > 0) |
| 4 | 50 | | { |
| 4 | 51 | | var root1Node = root1Queue.Dequeue(); |
| 4 | 52 | | var root2Node = root2Queue.Dequeue(); |
| | 53 | |
|
| 4 | 54 | | root1Node.val += root2Node.val; |
| | 55 | |
|
| 4 | 56 | | if (root1Node.left == null) |
| 2 | 57 | | { |
| 2 | 58 | | root1Node.left = root2Node.left; |
| 2 | 59 | | } |
| | 60 | | else |
| 2 | 61 | | { |
| 2 | 62 | | if (root2Node.left != null) |
| 1 | 63 | | { |
| 1 | 64 | | root1Queue.Enqueue(root1Node.left); |
| 1 | 65 | | root2Queue.Enqueue(root2Node.left); |
| 1 | 66 | | } |
| 2 | 67 | | } |
| | 68 | |
|
| 4 | 69 | | if (root1Node.right == null) |
| 3 | 70 | | { |
| 3 | 71 | | root1Node.right = root2Node.right; |
| 3 | 72 | | } |
| | 73 | | else |
| 1 | 74 | | { |
| 1 | 75 | | if (root2Node.right != null) |
| 1 | 76 | | { |
| 1 | 77 | | root1Queue.Enqueue(root1Node.right); |
| 1 | 78 | | root2Queue.Enqueue(root2Node.right); |
| 1 | 79 | | } |
| 1 | 80 | | } |
| 4 | 81 | | } |
| | 82 | |
|
| 2 | 83 | | return root1; |
| 5 | 84 | | } |
| | 85 | | } |