| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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 sealed 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) |
| | 12 | 27 | | { |
| | 12 | 28 | | if (root == null) |
| | 1 | 29 | | { |
| | 1 | 30 | | return false; |
| | | 31 | | } |
| | | 32 | | |
| | 11 | 33 | | var queue = new Queue<(int, TreeNode)>(); |
| | | 34 | | |
| | 11 | 35 | | queue.Enqueue((0, root)); |
| | | 36 | | |
| | 11 | 37 | | var previousVal = 0; |
| | 11 | 38 | | var currentLevel = 0; |
| | | 39 | | |
| | 61 | 40 | | while (queue.Count > 0) |
| | 58 | 41 | | { |
| | 58 | 42 | | var (nodeLevel, node) = queue.Dequeue(); |
| | | 43 | | |
| | 58 | 44 | | if (currentLevel != nodeLevel) |
| | 20 | 45 | | { |
| | 20 | 46 | | previousVal = 0; |
| | 20 | 47 | | currentLevel = nodeLevel; |
| | 20 | 48 | | } |
| | | 49 | | |
| | 58 | 50 | | if (nodeLevel % 2 == 0) |
| | 29 | 51 | | { |
| | 29 | 52 | | if (node.val % 2 == 0) |
| | 1 | 53 | | { |
| | 1 | 54 | | return false; |
| | | 55 | | } |
| | | 56 | | |
| | 28 | 57 | | if (previousVal != 0 && node.val >= previousVal) |
| | 1 | 58 | | { |
| | 1 | 59 | | return false; |
| | | 60 | | } |
| | 27 | 61 | | } |
| | | 62 | | else |
| | 29 | 63 | | { |
| | 29 | 64 | | if (node.val % 2 != 0) |
| | 6 | 65 | | { |
| | 6 | 66 | | return false; |
| | | 67 | | } |
| | | 68 | | |
| | 23 | 69 | | if (previousVal != 0 && node.val <= previousVal) |
| | 0 | 70 | | { |
| | 0 | 71 | | return false; |
| | | 72 | | } |
| | 23 | 73 | | } |
| | | 74 | | |
| | 50 | 75 | | previousVal = node.val; |
| | | 76 | | |
| | 50 | 77 | | if (node.right != null) |
| | 28 | 78 | | { |
| | 28 | 79 | | queue.Enqueue((nodeLevel + 1, node.right)); |
| | 28 | 80 | | } |
| | | 81 | | |
| | 50 | 82 | | if (node.left != null) |
| | 34 | 83 | | { |
| | 34 | 84 | | queue.Enqueue((nodeLevel + 1, node.left)); |
| | 34 | 85 | | } |
| | 50 | 86 | | } |
| | | 87 | | |
| | 3 | 88 | | return true; |
| | 12 | 89 | | } |
| | | 90 | | } |