-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path104.maximum-depth-of-binary-tree.ts
82 lines (65 loc) · 1.61 KB
/
104.maximum-depth-of-binary-tree.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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;
}
}
function maxDepthBFS(root: TreeNode | null): number {
if (root === null) {
return 0;
}
const queue = [root];
let depth = 0;
while (queue.length !== 0) {
const size = queue.length;
for (let i = 0; i < size; i++) {
const node = queue.shift();
if (node.left) {
queue.push(node.left);
}
if (node.right) {
queue.push(node.right);
}
}
depth += 1;
}
return depth;
}
function maxDepthDSF(root: TreeNode | null): number {
if (root === null) {
return 0;
}
const stack = [{ node: root, depth: 1 }];
let maxDepth = 0;
while (stack.length !== 0) {
const { node, depth } = stack.pop();
maxDepth = Math.max(maxDepth, depth);
if (node.right) {
stack.push({ node: node.right, depth: depth + 1 });
}
if (node.left) {
stack.push({ node: node.left, depth: depth + 1 });
}
}
return maxDepth;
}
function maxDepthRecursion(root: TreeNode | null): number {
if (root === null) {
return 0;
}
return (
1 + Math.max(maxDepthRecursion(root.left), maxDepthRecursion(root.right))
);
}
const tree = new TreeNode(
3,
new TreeNode(9),
new TreeNode(20, new TreeNode(15), new TreeNode(7))
);
console.log(maxDepthBFS(tree));
console.log(maxDepthDSF(tree));
console.log(maxDepthRecursion(tree));