< Summary

Information
Class: LeetCode.Algorithms.AddOneRowToTree.AddOneRowToTreeDepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AddOneRowToTree\AddOneRowToTreeDepthFirstSearch.cs
Line coverage
100%
Covered lines: 28
Uncovered lines: 0
Coverable lines: 28
Total lines: 66
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddOneRow(...)100%44100%
AddOneRow(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AddOneRowToTree\AddOneRowToTreeDepthFirstSearch.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.AddOneRowToTree;
 15
 16/// <inheritdoc />
 17public class AddOneRowToTreeDepthFirstSearch : IAddOneRowToTree
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n), where n is the number of nodes in the tree
 21    ///     Space complexity - O(h), where h is the height of the tree
 22    /// </summary>
 23    /// <param name="root"></param>
 24    /// <param name="val"></param>
 25    /// <param name="depth"></param>
 26    /// <returns></returns>
 27    public TreeNode? AddOneRow(TreeNode? root, int val, int depth)
 1528    {
 1529        if (root == null)
 130        {
 131            return null;
 32        }
 33
 1434        if (depth == 1)
 235        {
 236            return new TreeNode(val, root);
 37        }
 38
 1239        AddOneRow(root, val, depth, 1);
 40
 1241        return root;
 1542    }
 43
 44    private static void AddOneRow(TreeNode treeNode, int val, int depth, int level)
 4045    {
 4046        if (level + 1 == depth)
 1447        {
 1448            treeNode.left = new TreeNode(val, treeNode.left);
 1449            treeNode.right = new TreeNode(val, null, treeNode.right);
 1450        }
 51        else
 2652        {
 2653            level++;
 54
 2655            if (treeNode.left != null)
 2056            {
 2057                AddOneRow(treeNode.left, val, depth, level);
 2058            }
 59
 2660            if (treeNode.right != null)
 861            {
 862                AddOneRow(treeNode.right, val, depth, level);
 863            }
 2664        }
 4065    }
 66}