generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem21.cs
26 lines (23 loc) · 781 Bytes
/
Problem21.cs
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/maximum-depth-of-binary-tree/">Maximum Depth of Binary Tree</see>.
/// </summary>
public static class Problem21
{
/// <summary>
/// Given the root of a binary tree, return its maximum depth.
/// Time complexity: O(n).
/// Space complexity: O(n).
/// </summary>
/// <param name="root">Binary tree root.</param>
/// <returns>Maximum depth of the binary tree.</returns>
public static int MaxDepth(TreeNode? root) => Traverse(root, 0);
private static int Traverse(TreeNode? node, int depth)
{
if (node == null)
{
return depth;
}
return Math.Max(Traverse(node.Left, depth + 1), Traverse(node.Right, depth + 1));
}
}