< Summary

Information
Class: LeetCode.Algorithms.InvertBinaryTree.InvertBinaryTreeDepthFirstSearchRecursive
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\InvertBinaryTree\InvertBinaryTreeDepthFirstSearchRecursive.cs
Line coverage
86%
Covered lines: 13
Uncovered lines: 2
Coverable lines: 15
Total lines: 49
Line coverage: 86.6%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
InvertTree(...)100%22100%
Invert(...)50%2275%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\InvertBinaryTree\InvertBinaryTreeDepthFirstSearchRecursive.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.InvertBinaryTree;
 15
 16/// <inheritdoc />
 17public class InvertBinaryTreeDepthFirstSearchRecursive : IInvertBinaryTree
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(h), where h is the height of the tree
 22    /// </summary>
 23    /// <param name="root"></param>
 24    /// <returns></returns>
 25    public TreeNode? InvertTree(TreeNode? root)
 5526    {
 5527        if (root == null)
 3128        {
 3129            return null;
 30        }
 31
 2432        Invert(root);
 33
 2434        return root;
 5535    }
 36
 37    private void Invert(TreeNode? root)
 2438    {
 2439        if (root == null)
 040        {
 041            return;
 42        }
 43
 2444        (root.left, root.right) = (root.right, root.left);
 45
 2446        InvertTree(root.left);
 2447        InvertTree(root.right);
 2448    }
 49}