generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem38.cs
More file actions
26 lines (23 loc) · 706 Bytes
/
Problem38.cs
File metadata and controls
26 lines (23 loc) · 706 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/invert-binary-tree/">Invert Binary Tree</see>.
/// </summary>
public static class Problem38
{
/// <summary>
/// Given the root of a binary tree, invert the tree, and return its root.
/// Time complexity: O(n).
/// Space complexity: O(n).
/// </summary>
/// <param name="root">Binary tree to invert.</param>
/// <returns>Inverted binary tree.</returns>
public static TreeNode? InvertTree(TreeNode? root)
{
if (root == null)
{
return null;
}
(root.Left, root.Right) = (InvertTree(root.Right), InvertTree(root.Left));
return root;
}
}