< 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
 283629    public TreeNode(int? val = null, TreeNode? left = null, TreeNode? right = null)
 283630    {
 283631        this.left = left;
 283632        this.right = right;
 283633        this.val = val ?? 0;
 283634    }
 35
 36    public static TreeNode ToTreeNodeOrThrow(IEnumerable<int?> values)
 7437    {
 7438        return ToTreeNode(values) ?? throw new TreeNodeBuildException();
 7439    }
 40
 41    public static TreeNode? ToTreeNode(IEnumerable<int?> values)
 56642    {
 56643        using var enumerator = values.GetEnumerator();
 44
 56645        if (!enumerator.MoveNext() || enumerator.Current == null)
 6646        {
 6647            return null;
 48        }
 49
 50050        var root = new TreeNode(enumerator.Current.Value);
 50051        var queue = new Queue<TreeNode>();
 52
 50053        queue.Enqueue(root);
 54
 191855        while (queue.Count > 0)
 191856        {
 191857            var current = queue.Dequeue();
 58
 191859            if (enumerator.MoveNext())
 153360            {
 153361                if (enumerator.Current.HasValue)
 116462                {
 116463                    current.left = new TreeNode(enumerator.Current.Value);
 64
 116465                    queue.Enqueue(current.left);
 116466                }
 153367            }
 68            else
 38569            {
 38570                break;
 71            }
 72
 153373            if (enumerator.MoveNext())
 141874            {
 141875                if (!enumerator.Current.HasValue)
 36876                {
 36877                    continue;
 78                }
 79
 105080                current.right = new TreeNode(enumerator.Current.Value);
 81
 105082                queue.Enqueue(current.right);
 105083            }
 84            else
 11585            {
 11586                break;
 87            }
 105088        }
 89
 50090        return root;
 56691    }
 92}