< Summary

Information
Class: LeetCode.Algorithms.MergeTwoBinaryTrees.MergeTwoBinaryTreesDepthFirstSearchRecursive
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MergeTwoBinaryTrees\MergeTwoBinaryTreesDepthFirstSearchRecursive.cs
Line coverage
100%
Covered lines: 15
Uncovered lines: 0
Coverable lines: 15
Total lines: 49
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
MergeTrees(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MergeTwoBinaryTrees\MergeTwoBinaryTreesDepthFirstSearchRecursive.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.MergeTwoBinaryTrees;
 15
 16/// <inheritdoc />
 17public class MergeTwoBinaryTreesDepthFirstSearchRecursive : IMergeTwoBinaryTrees
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n + m)
 21    ///     Space complexity - O(n + m) for a skewed trees, O(log n + log m) for a balanced trees
 22    /// </summary>
 23    /// <param name="root1"></param>
 24    /// <param name="root2"></param>
 25    /// <returns></returns>
 26    public TreeNode? MergeTrees(TreeNode? root1, TreeNode? root2)
 1327    {
 1328        if (root1 == null && root2 == null)
 329        {
 330            return null;
 31        }
 32
 1033        if (root1 == null)
 434        {
 435            return root2;
 36        }
 37
 638        if (root2 == null)
 239        {
 240            return root1;
 41        }
 42
 443        root1.val += root2.val;
 444        root1.left = MergeTrees(root1.left, root2.left);
 445        root1.right = MergeTrees(root1.right, root2.right);
 46
 447        return root1;
 1348    }
 49}