| | 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.CousinsInBinaryTree2; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class CousinsInBinaryTree2BreadthFirstSearch : ICousinsInBinaryTree2 |
| | 18 | | { |
| | 19 | | public TreeNode ReplaceValueInTree(TreeNode root) |
| 2 | 20 | | { |
| 2 | 21 | | var queue = new Queue<TreeNode>(); |
| | 22 | |
|
| 2 | 23 | | queue.Enqueue(root); |
| | 24 | |
|
| 2 | 25 | | var levelSums = new List<int>(); |
| | 26 | |
|
| 7 | 27 | | while (queue.Count > 0) |
| 5 | 28 | | { |
| 5 | 29 | | var levelSum = 0; |
| 5 | 30 | | var levelSize = queue.Count; |
| | 31 | |
|
| 28 | 32 | | for (var i = 0; i < levelSize; i++) |
| 9 | 33 | | { |
| 9 | 34 | | var currentNode = queue.Dequeue(); |
| | 35 | |
|
| 9 | 36 | | levelSum += currentNode.val; |
| | 37 | |
|
| 9 | 38 | | if (currentNode.left != null) |
| 3 | 39 | | { |
| 3 | 40 | | queue.Enqueue(currentNode.left); |
| 3 | 41 | | } |
| | 42 | |
|
| 9 | 43 | | if (currentNode.right != null) |
| 4 | 44 | | { |
| 4 | 45 | | queue.Enqueue(currentNode.right); |
| 4 | 46 | | } |
| 9 | 47 | | } |
| | 48 | |
|
| 5 | 49 | | levelSums.Add(levelSum); |
| 5 | 50 | | } |
| | 51 | |
|
| 2 | 52 | | queue.Enqueue(root); |
| | 53 | |
|
| 2 | 54 | | var levelIndex = 1; |
| | 55 | |
|
| 2 | 56 | | root.val = 0; |
| | 57 | |
|
| 7 | 58 | | while (queue.Count > 0) |
| 5 | 59 | | { |
| 5 | 60 | | var levelSize = queue.Count; |
| | 61 | |
|
| 28 | 62 | | for (var i = 0; i < levelSize; i++) |
| 9 | 63 | | { |
| 9 | 64 | | var currentNode = queue.Dequeue(); |
| | 65 | |
|
| 9 | 66 | | var sum = (currentNode.left?.val ?? 0) + (currentNode.right?.val ?? 0); |
| | 67 | |
|
| 9 | 68 | | if (currentNode.left != null) |
| 3 | 69 | | { |
| 3 | 70 | | currentNode.left.val = levelSums[levelIndex] - sum; |
| | 71 | |
|
| 3 | 72 | | queue.Enqueue(currentNode.left); |
| 3 | 73 | | } |
| | 74 | |
|
| 9 | 75 | | if (currentNode.right != null) |
| 4 | 76 | | { |
| 4 | 77 | | currentNode.right.val = levelSums[levelIndex] - sum; |
| | 78 | |
|
| 4 | 79 | | queue.Enqueue(currentNode.right); |
| 4 | 80 | | } |
| 9 | 81 | | } |
| | 82 | |
|
| 5 | 83 | | levelIndex++; |
| 5 | 84 | | } |
| | 85 | |
|
| 2 | 86 | | return root; |
| 2 | 87 | | } |
| | 88 | | } |