< Summary

Information
Class: LeetCode.Core.Models.TreeNode
Assembly: LeetCode.Core
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode.Core\Models\TreeNode.cs
Line coverage
100%
Covered lines: 43
Uncovered lines: 0
Coverable lines: 43
Total lines: 92
Line coverage: 100%
Branch coverage
93%
Covered branches: 15
Total branches: 16
Branch coverage: 93.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ToTreeNodeOrThrow(...)50%22100%
ToTreeNode(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode.Core\Models\TreeNode.cs

#LineLine coverage
 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// ReSharper disable InconsistentNaming
 13
 14using LeetCode.Core.Exceptions;
 15
 16namespace LeetCode.Core.Models;
 17
 18/// <summary>
 19///     Definition for a binary tree node
 20/// </summary>
 21public sealed class TreeNode
 22{
 23    public TreeNode? left;
 24
 25    public TreeNode? right;
 26
 27    public int val;
 28
 284029    public TreeNode(int? val = null, TreeNode? left = null, TreeNode? right = null)
 284030    {
 284031        this.left = left;
 284032        this.right = right;
 284033        this.val = val ?? 0;
 284034    }
 35
 36    public static TreeNode ToTreeNodeOrThrow(IEnumerable<int?> values)
 7637    {
 7638        return ToTreeNode(values) ?? throw new TreeNodeBuildException();
 7639    }
 40
 41    public static TreeNode? ToTreeNode(IEnumerable<int?> values)
 56842    {
 56843        using var enumerator = values.GetEnumerator();
 44
 56845        if (!enumerator.MoveNext() || enumerator.Current == null)
 6646        {
 6647            return null;
 48        }
 49
 50250        var root = new TreeNode(enumerator.Current.Value);
 50251        var queue = new Queue<TreeNode>();
 52
 50253        queue.Enqueue(root);
 54
 192255        while (queue.Count > 0)
 192256        {
 192257            var current = queue.Dequeue();
 58
 192259            if (enumerator.MoveNext())
 153560            {
 153561                if (enumerator.Current.HasValue)
 116462                {
 116463                    current.left = new TreeNode(enumerator.Current.Value);
 64
 116465                    queue.Enqueue(current.left);
 116466                }
 153567            }
 68            else
 38769            {
 38770                break;
 71            }
 72
 153573            if (enumerator.MoveNext())
 142074            {
 142075                if (!enumerator.Current.HasValue)
 36876                {
 36877                    continue;
 78                }
 79
 105280                current.right = new TreeNode(enumerator.Current.Value);
 81
 105282                queue.Enqueue(current.right);
 105283            }
 84            else
 11585            {
 11586                break;
 87            }
 105288        }
 89
 50290        return root;
 56891    }
 92}