-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path33-104.ts
26 lines (22 loc) · 895 Bytes
/
33-104.ts
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
// https://leetcode.com/problems/maximum-depth-of-binary-tree/?envType=study-plan-v2&envId=leetcode-75
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
// @ts-nocheck
const depthFirstMaxDepth = (root: TreeNode | null, depth: number): number => {
if (root === null) return depth;
const leftDepth = depthFirstMaxDepth(root.left, depth + 1);
const rightDepth = depthFirstMaxDepth(root.right, depth + 1);
return Math.max(leftDepth, rightDepth);
};
const maxDepth = (root: TreeNode | null): number => depthFirstMaxDepth(root, 0);