Skip to content

Commit 758338a

Browse files
committed
Add MaxDepth method to calculate binary tree depth
This commit introduces a new method `MaxDepth` in the `Solution` class under the `LeetCodeNote` namespace. The method computes the maximum depth of a binary tree using recursion, handling null input by returning 0. It evaluates the depths of the left and right subtrees and returns the greater value plus one.
1 parent 3096907 commit 758338a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace LeetCodeNote;
4+
5+
public static partial class Solution
6+
{
7+
public int MaxDepth(TreeNode root)
8+
{
9+
if (root == null) return 0;
10+
11+
int L = MaxDepth(root.left);
12+
int R = MaxDepth(root.right);
13+
return Math.Max(L, R) + 1;
14+
}
15+
}

0 commit comments

Comments
 (0)