< Summary

Information
Class: LeetCode.Algorithms.CreateBinaryTreeFromDescriptions.CreateBinaryTreeFromDescriptionsDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CreateBinaryTreeFromDescriptions\CreateBinaryTreeFromDescriptionsDictionary.cs
Line coverage
97%
Covered lines: 34
Uncovered lines: 1
Coverable lines: 35
Total lines: 71
Line coverage: 97.1%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CreateBinaryTree(...)91.66%121297.14%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CreateBinaryTreeFromDescriptions\CreateBinaryTreeFromDescriptionsDictionary.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.CreateBinaryTreeFromDescriptions;
 15
 16/// <inheritdoc />
 17public class CreateBinaryTreeFromDescriptionsDictionary : ICreateBinaryTreeFromDescriptions
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(n)
 22    /// </summary>
 23    /// <param name="descriptions"></param>
 24    /// <returns></returns>
 25    public TreeNode? CreateBinaryTree(int[][] descriptions)
 226    {
 227        var nodeMap = new Dictionary<int, TreeNode>();
 228        var children = new HashSet<int>();
 29
 2230        foreach (var description in descriptions)
 831        {
 832            var parentValue = description[0];
 833            var childValue = description[1];
 834            var isLeft = description[2] == 1;
 35
 836            if (!nodeMap.ContainsKey(parentValue))
 337            {
 338                nodeMap[parentValue] = new TreeNode(parentValue);
 339            }
 40
 841            if (!nodeMap.ContainsKey(childValue))
 742            {
 743                nodeMap[childValue] = new TreeNode(childValue);
 744            }
 45
 846            var parent = nodeMap[parentValue];
 847            var child = nodeMap[childValue];
 48
 849            if (isLeft)
 550            {
 551                parent.left = child;
 552            }
 53            else
 354            {
 355                parent.right = child;
 356            }
 57
 858            children.Add(childValue);
 859        }
 60
 1461        foreach (var key in nodeMap.Keys)
 562        {
 563            if (!children.Contains(key))
 264            {
 265                return nodeMap[key];
 66            }
 367        }
 68
 069        return null;
 270    }
 71}