| | 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.EvenOddTree; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class EvenOddTreeBreadthFirst : IEvenOddTree |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(n), where n is the number of nodes in the binary tree |
| | 21 | | /// Space complexity - O(n) for a skewed tree, where n is the number of nodes in the binary tree. O(w) for a bal |
| | 22 | | /// tree, where w is the maximum width of the tree |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="root"></param> |
| | 25 | | /// <returns></returns> |
| | 26 | | public bool IsEvenOddTree(TreeNode? root) |
| 13 | 27 | | { |
| 13 | 28 | | if (root == null) |
| 1 | 29 | | { |
| 1 | 30 | | return false; |
| | 31 | | } |
| | 32 | |
|
| 12 | 33 | | var queue = new Queue<(int, TreeNode)>(); |
| | 34 | |
|
| 12 | 35 | | queue.Enqueue((0, root)); |
| | 36 | |
|
| 12 | 37 | | var previousVal = 0; |
| 12 | 38 | | var currentLevel = 0; |
| | 39 | |
|
| 67 | 40 | | while (queue.Count > 0) |
| 64 | 41 | | { |
| 64 | 42 | | var (nodeLevel, node) = queue.Dequeue(); |
| | 43 | |
|
| 64 | 44 | | if (currentLevel != nodeLevel) |
| 22 | 45 | | { |
| 22 | 46 | | previousVal = 0; |
| 22 | 47 | | currentLevel = nodeLevel; |
| 22 | 48 | | } |
| | 49 | |
|
| 64 | 50 | | if (nodeLevel % 2 == 0) |
| 33 | 51 | | { |
| 33 | 52 | | if (node.val % 2 == 0) |
| 2 | 53 | | { |
| 2 | 54 | | return false; |
| | 55 | | } |
| | 56 | |
|
| 31 | 57 | | if (previousVal != 0 && node.val >= previousVal) |
| 1 | 58 | | { |
| 1 | 59 | | return false; |
| | 60 | | } |
| 30 | 61 | | } |
| | 62 | | else |
| 31 | 63 | | { |
| 31 | 64 | | if (node.val % 2 != 0) |
| 6 | 65 | | { |
| 6 | 66 | | return false; |
| | 67 | | } |
| | 68 | |
|
| 25 | 69 | | if (previousVal != 0 && node.val <= previousVal) |
| 0 | 70 | | { |
| 0 | 71 | | return false; |
| | 72 | | } |
| 25 | 73 | | } |
| | 74 | |
|
| 55 | 75 | | previousVal = node.val; |
| | 76 | |
|
| 55 | 77 | | if (node.right != null) |
| 32 | 78 | | { |
| 32 | 79 | | queue.Enqueue((nodeLevel + 1, node.right)); |
| 32 | 80 | | } |
| | 81 | |
|
| 55 | 82 | | if (node.left != null) |
| 37 | 83 | | { |
| 37 | 84 | | queue.Enqueue((nodeLevel + 1, node.left)); |
| 37 | 85 | | } |
| 55 | 86 | | } |
| | 87 | |
|
| 3 | 88 | | return true; |
| 13 | 89 | | } |
| | 90 | | } |