< Summary

Information
Class: LeetCode.Algorithms.MaximumProductOfSplittedBinaryTree.MaximumProductOfSplittedBinaryTreeDepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaximumProductOfSplittedBinaryTree\MaximumProductOfSplittedBinaryTreeDepthFirstSearch.cs
Line coverage
100%
Covered lines: 32
Uncovered lines: 0
Coverable lines: 32
Total lines: 75
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MaxProduct(...)100%11100%
GetTotalSum()100%44100%
FindMaximumProduct()100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaximumProductOfSplittedBinaryTree\MaximumProductOfSplittedBinaryTreeDepthFirstSearch.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
 12using LeetCode.Core.Models;
 13
 14namespace LeetCode.Algorithms.MaximumProductOfSplittedBinaryTree;
 15
 16/// <inheritdoc />
 17public sealed class MaximumProductOfSplittedBinaryTreeDepthFirstSearch : IMaximumProductOfSplittedBinaryTree
 18{
 19    private const int Modulo = 1_000_000_007;
 20
 21    /// <summary>
 22    ///     Time complexity - O(n), where n is the number of nodes in the binary tree
 23    ///     Space complexity - O(h), where h is the height of the tree
 24    /// </summary>
 25    /// <param name="root"></param>
 26    /// <returns></returns>
 27    public int MaxProduct(TreeNode root)
 228    {
 229        long maximumProduct = 0;
 30
 231        var totalSum = GetTotalSum(root);
 32
 233        FindMaximumProduct(root);
 34
 235        return (int)(maximumProduct % Modulo);
 36
 37        long GetTotalSum(TreeNode treeNode)
 1238        {
 1239            long sum = treeNode.val;
 40
 1241            if (treeNode.left != null)
 542            {
 543                sum += GetTotalSum(treeNode.left);
 544            }
 45
 1246            if (treeNode.right != null)
 547            {
 548                sum += GetTotalSum(treeNode.right);
 549            }
 50
 1251            return sum;
 1252        }
 53
 54        long FindMaximumProduct(TreeNode treeNode)
 1255        {
 1256            long sum = treeNode.val;
 57
 1258            if (treeNode.left != null)
 559            {
 560                sum += FindMaximumProduct(treeNode.left);
 561            }
 62
 1263            if (treeNode.right != null)
 564            {
 565                sum += FindMaximumProduct(treeNode.right);
 566            }
 67
 1268            var product = sum * (totalSum - sum);
 69
 1270            maximumProduct = Math.Max(maximumProduct, product);
 71
 1272            return sum;
 1273        }
 274    }
 75}