< Summary

Information
Class: LeetCode.Algorithms.EvenOddTree.EvenOddTreeBreadthFirst
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\EvenOddTree\EvenOddTreeBreadthFirst.cs
Line coverage
95%
Covered lines: 43
Uncovered lines: 2
Coverable lines: 45
Total lines: 90
Line coverage: 95.5%
Branch coverage
95%
Covered branches: 23
Total branches: 24
Branch coverage: 95.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsEvenOddTree(...)95.83%242495.55%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\EvenOddTree\EvenOddTreeBreadthFirst.cs

#LineLine coverage
 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
 12using LeetCode.Core.Models;
 13
 14namespace LeetCode.Algorithms.EvenOddTree;
 15
 16/// <inheritdoc />
 17public 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)
 1327    {
 1328        if (root == null)
 129        {
 130            return false;
 31        }
 32
 1233        var queue = new Queue<(int, TreeNode)>();
 34
 1235        queue.Enqueue((0, root));
 36
 1237        var previousVal = 0;
 1238        var currentLevel = 0;
 39
 6740        while (queue.Count > 0)
 6441        {
 6442            var (nodeLevel, node) = queue.Dequeue();
 43
 6444            if (currentLevel != nodeLevel)
 2245            {
 2246                previousVal = 0;
 2247                currentLevel = nodeLevel;
 2248            }
 49
 6450            if (nodeLevel % 2 == 0)
 3351            {
 3352                if (node.val % 2 == 0)
 253                {
 254                    return false;
 55                }
 56
 3157                if (previousVal != 0 && node.val >= previousVal)
 158                {
 159                    return false;
 60                }
 3061            }
 62            else
 3163            {
 3164                if (node.val % 2 != 0)
 665                {
 666                    return false;
 67                }
 68
 2569                if (previousVal != 0 && node.val <= previousVal)
 070                {
 071                    return false;
 72                }
 2573            }
 74
 5575            previousVal = node.val;
 76
 5577            if (node.right != null)
 3278            {
 3279                queue.Enqueue((nodeLevel + 1, node.right));
 3280            }
 81
 5582            if (node.left != null)
 3783            {
 3784                queue.Enqueue((nodeLevel + 1, node.left));
 3785            }
 5586        }
 87
 388        return true;
 1389    }
 90}