-
Notifications
You must be signed in to change notification settings - Fork 0
/
No_104.ts
39 lines (32 loc) · 1.01 KB
/
No_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
27
28
29
30
31
32
33
34
35
36
37
38
39
namespace Leetcode {
export 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)
}
};
export class No_104 {
maxDepth(root: TreeNode | null): number {
if (root === null)
return 0;
let c = 1, l = 0;
let tree: TreeNode[] = [root];
while (tree.length != 0) {
let cur = tree.shift();
if (cur.left != null)
tree.push(cur.left);
if (cur.right != null)
tree.push(cur.right);
if (--c == 0) {
l++;
c = tree.length;
}
}
return l;
};
}
}