< Summary

Information
Class: LeetCode.Algorithms.ConstructBinaryTreeFromPreorderAndPostorderTraversal.ConstructBinaryTreeFromPreorderAndPostorderTraversalRecursive
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ConstructBinaryTreeFromPreorderAndPostorderTraversal\ConstructBinaryTreeFromPreorderAndPostorderTraversalRecursive.cs
Line coverage
90%
Covered lines: 19
Uncovered lines: 2
Coverable lines: 21
Total lines: 60
Line coverage: 90.4%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ConstructFromPrePost(...)100%11100%
ConstructFromPrePost(...)83.33%6688.88%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ConstructBinaryTreeFromPreorderAndPostorderTraversal\ConstructBinaryTreeFromPreorderAndPostorderTraversalRecursive.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.ConstructBinaryTreeFromPreorderAndPostorderTraversal;
 15
 16/// <inheritdoc />
 17public class ConstructBinaryTreeFromPreorderAndPostorderTraversalRecursive :
 18    IConstructBinaryTreeFromPreorderAndPostorderTraversal
 19{
 20    /// <summary>
 21    ///     Time complexity - O(n)
 22    ///     Space complexity - O(n)
 23    /// </summary>
 24    /// <param name="preorder"></param>
 25    /// <param name="postorder"></param>
 26    /// <returns></returns>
 27    public TreeNode ConstructFromPrePost(int[] preorder, int[] postorder)
 228    {
 229        return ConstructFromPrePost(preorder, postorder, 0, preorder.Length - 1, 0)!;
 230    }
 31
 32    private static TreeNode? ConstructFromPrePost(int[] preorder, int[] postorder, int preorderStart, int preorderEnd,
 33        int postorderStart)
 834    {
 835        if (preorderStart > preorderEnd)
 036        {
 037            return null;
 38        }
 39
 840        if (preorderStart == preorderEnd)
 541        {
 542            return new TreeNode(preorder[preorderStart]);
 43        }
 44
 345        var numOfNodesInLeft = 1;
 46
 547        while (postorder[postorderStart + numOfNodesInLeft - 1] != preorder[preorderStart + 1])
 248        {
 249            numOfNodesInLeft++;
 250        }
 51
 352        var left = ConstructFromPrePost(preorder, postorder, preorderStart + 1, preorderStart + numOfNodesInLeft,
 353            postorderStart);
 54
 355        var right = ConstructFromPrePost(preorder, postorder, preorderStart + numOfNodesInLeft + 1, preorderEnd,
 356            postorderStart + numOfNodesInLeft);
 57
 358        return new TreeNode(preorder[preorderStart], left, right);
 859    }
 60}