< Summary

Information
Class: LeetCode.Algorithms.MergeTwoBinaryTrees.MergeTwoBinaryTreesDepthFirstSearchStack
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MergeTwoBinaryTrees\MergeTwoBinaryTreesDepthFirstSearchStack.cs
Line coverage
100%
Covered lines: 44
Uncovered lines: 0
Coverable lines: 44
Total lines: 85
Line coverage: 100%
Branch coverage
100%
Covered branches: 20
Total branches: 20
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%2020100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MergeTwoBinaryTrees\MergeTwoBinaryTreesDepthFirstSearchStack.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 MergeTwoBinaryTreesDepthFirstSearchStack : 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)
 527    {
 528        if (root1 == null && root2 == null)
 129        {
 130            return null;
 31        }
 32
 433        if (root1 == null)
 134        {
 135            return root2;
 36        }
 37
 338        if (root2 == null)
 139        {
 140            return root1;
 41        }
 42
 243        var root1Stack = new Stack<TreeNode>();
 244        var root2Stack = new Stack<TreeNode>();
 45
 246        root1Stack.Push(root1);
 247        root2Stack.Push(root2);
 48
 649        while (root1Stack.Count > 0 || root2Stack.Count > 0)
 450        {
 451            var root1Node = root1Stack.Pop();
 452            var root2Node = root2Stack.Pop();
 53
 454            root1Node.val += root2Node.val;
 55
 456            if (root1Node.left == null)
 257            {
 258                root1Node.left = root2Node.left;
 259            }
 60            else
 261            {
 262                if (root2Node.left != null)
 163                {
 164                    root1Stack.Push(root1Node.left);
 165                    root2Stack.Push(root2Node.left);
 166                }
 267            }
 68
 469            if (root1Node.right == null)
 370            {
 371                root1Node.right = root2Node.right;
 372            }
 73            else
 174            {
 175                if (root2Node.right != null)
 176                {
 177                    root1Stack.Push(root1Node.right);
 178                    root2Stack.Push(root2Node.right);
 179                }
 180            }
 481        }
 82
 283        return root1;
 584    }
 85}