We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 85c5b26 commit 8ac93baCopy full SHA for 8ac93ba
maximum-depth-of-binary-tree/sukyoungshin.ts
@@ -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