< 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)
 1227    {
 1228        if (root == null)
 129        {
 130            return false;
 31        }
 32
 1133        var queue = new Queue<(int, TreeNode)>();
 34
 1135        queue.Enqueue((0, root));
 36
 1137        var previousVal = 0;
 1138        var currentLevel = 0;
 39
 6140        while (queue.Count > 0)
 5841        {
 5842            var (nodeLevel, node) = queue.Dequeue();
 43
 5844            if (currentLevel != nodeLevel)
 2045            {
 2046                previousVal = 0;
 2047                currentLevel = nodeLevel;
 2048            }
 49
 5850            if (nodeLevel % 2 == 0)
 2951            {
 2952                if (node.val % 2 == 0)
 153                {
 154                    return false;
 55                }
 56
 2857                if (previousVal != 0 && node.val >= previousVal)
 158                {
 159                    return false;
 60                }
 2761            }
 62            else
 2963            {
 2964                if (node.val % 2 != 0)
 665                {
 666                    return false;
 67                }
 68
 2369                if (previousVal != 0 && node.val <= previousVal)
 070                {
 071                    return false;
 72                }
 2373            }
 74
 5075            previousVal = node.val;
 76
 5077            if (node.right != null)
 2878            {
 2879                queue.Enqueue((nodeLevel + 1, node.right));
 2880            }
 81
 5082            if (node.left != null)
 3483            {
 3484                queue.Enqueue((nodeLevel + 1, node.left));
 3485            }
 5086        }
 87
 388        return true;
 1289    }
 90}