-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTreePreorderTraversal.ts
125 lines (113 loc) · 2.99 KB
/
BinaryTreePreorderTraversal.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/
// Author : squxq
// Date : 2023-11-13
/*****************************************************************************************************
*
* Given the root of a binary tree, return the preorder traversal of its nodes' values.
*
* Example 1:
*
* Input: root = [1,null,2,3]
* Output: [1,2,3]
*
* Example 2:
*
* Input: root = []
* Output: []
*
* Example 3:
*
* Input: root = [1]
* Output: [1]
*
* Constraints:
*
* The number of nodes in the tree is in the range [0, 100].
* -100 <= Node.val <= 100
*
* Follow up: Recursive solution is trivial, could you do it iteratively?
******************************************************************************************************/
export type BT = NodeTree | null;
export class NodeTree {
val: number;
left: BT;
right: BT;
constructor(val?: number, left?: BT, right?: BT) {
this.val = val ?? 0;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
}
/**
* BT -> number[]
* given the root of a binary tree, root, return the pre-order traversal of its node's values
* Stub:
function preorderTraversal(root: BT): number[] {return []}
* Tests:
* I: root = [1,null,2,3] -> O: [1,2,3]
* I: root = [] -> O: []
* I: root = [1] -> O: [1]
* Template:
function preorderTraversal(root: BT): number[] {
if (root === null) {return (...)}
else {
return (... (root.val)
(preorderTraversal(root.left))
(preorderTraversal(root.right)))
}
}
* CONSTRAINTS:
* - The number of nodes in the tree is in the range [0, 100].
* - -100 <= Node.val <= 100
*/
/**
* RECURSIVE SOLUTION 1
function preorderTraversal(root: BT): number[] {
const ans: number[] = []
function traversal(node: BT): void {
if (node !== null) {
ans.push(node.val)
traversal(node.left)
traversal(node.right)
}
}
traversal(root)
return ans
};
*/
/**
* RECURSIVE SOLUTION 2
function preorderTraversal(root: BT): number[] {
if (root === null) {return []}
return [root.val, ...preorderTraversal(root.left),
...preorderTraversal(root.right)]
}
*/
/**
* RECURSIVE SOLUTION 3
function preorderTraversal(root: BT, ans: number[] = []): number [] {
if (root === null) {return ans}
else {
ans.push(root.val)
ans = preorderTraversal(root.left, ans)
ans = preorderTraversal(root.right, ans)
return ans
}
}
*/
export function preorderTraversal(root: BT): number[] {
const ans: number[] = [];
const stack: BT[] = [];
let current: BT = root;
while (stack.length > 0 || current !== null) {
if (current !== null) {
ans.push(current.val);
stack.push(current);
current = current.left;
} else {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
current = stack.pop()!.right;
}
}
return ans;
}