Skip to content

Commit 8ac93ba

Browse files
committed
add: #227 Maximum Depth of Binary Tree
1 parent 85c5b26 commit 8ac93ba

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class TreeNode {
2+
val: number;
3+
left: TreeNode | null;
4+
right: TreeNode | null;
5+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
6+
this.val = val === undefined ? 0 : val;
7+
this.left = left === undefined ? null : left;
8+
this.right = right === undefined ? null : right;
9+
}
10+
}
11+
12+
function maxDepth(root: TreeNode | null): number {
13+
if (!root) return 0;
14+
15+
const leftDepth = maxDepth(root.left);
16+
const rightDepth = maxDepth(root.right);
17+
18+
return Math.max(leftDepth, rightDepth) + 1;
19+
};

0 commit comments

Comments
 (0)