< Summary

Information
Class: LeetCode.Algorithms.EvaluateBooleanBinaryTree.EvaluateBooleanBinaryTreeDepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\EvaluateBooleanBinaryTree\EvaluateBooleanBinaryTreeDepthFirstSearch.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 69
Line coverage: 100%
Branch coverage
95%
Covered branches: 19
Total branches: 20
Branch coverage: 95%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
EvaluateTree(...)50%22100%
GetTreeEvaluation(...)100%1818100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\EvaluateBooleanBinaryTree\EvaluateBooleanBinaryTreeDepthFirstSearch.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.EvaluateBooleanBinaryTree;
 15
 16/// <inheritdoc />
 17public class EvaluateBooleanBinaryTreeDepthFirstSearch : IEvaluateBooleanBinaryTree
 18{
 19    private const int OrOperation = 2;
 20    private const int AndOperation = 3;
 21
 22    /// <summary>
 23    ///     Time complexity - O(n)
 24    ///     Space complexity - O(n) for a skewed tree, O(n log n) for a balanced tree
 25    /// </summary>
 26    /// <param name="root"></param>
 27    /// <returns></returns>
 28    public bool EvaluateTree(TreeNode? root)
 829    {
 830        return root != null && GetTreeEvaluation(root);
 831    }
 32
 33    private static bool GetTreeEvaluation(TreeNode root)
 3034    {
 3035        if (root.left == null && root.right == null)
 1536        {
 1537            return root.val == 1;
 38        }
 39
 1540        var left = false;
 41
 1542        if (root.left != null)
 1543        {
 1544            left = GetTreeEvaluation(root.left);
 45
 1546            switch (root.val)
 47            {
 848                case OrOperation when left:
 449                    return true;
 750                case AndOperation when !left:
 451                    return false;
 52            }
 753        }
 54
 755        var right = false;
 56
 757        if (root.right != null)
 758        {
 759            right = GetTreeEvaluation(root.right);
 760        }
 61
 762        if (root.val == OrOperation)
 463        {
 464            return left | right;
 65        }
 66
 367        return left & right;
 3068    }
 69}