-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path144. Binary Tree Preorder Transversal.js
151 lines (128 loc) · 3.1 KB
/
144. Binary Tree Preorder Transversal.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
class TreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function dfs(root) {
if (root === null) {
return root;
}
let stack = [];
stack.push(root);
while (stack.length !== 0) {
let curr = stack.pop();
if (curr.right !== null) {
stack.push(curr.right);
}
if (curr.left !== null) {
stack.push(curr.left);
}
console.log(curr.value);
}
}
/***************************************************** */
function dfs_recursion(root) {
// base case when you hit the bottom of the tree
if (root === null) {
return root;
}
// recursive case
console.log(root.value);
if (root.left !== null) {
dfs_recursion(root.left);
}
if (root.right !== null) {
dfs_recursion(root.right);
}
}
function dfs_recursion2(root) {
let result = [];
function helper(root) {
// base case when you hit the bottom of the tree
if (root === null) {
return;
}
// recursive case
result.push(root.value);
if (root.left !== null) {
helper(root.left);
}
if (root.right !== null) {
helper(root.right);
}
}
helper(root);
return result;
}
/***************************************************** */
class Guide {
constructor(operation, node) {
this.operation = operation; // 0: visit, 1: print
this.node = node;
}
}
function preorder_iteration(root) {
let stack = [];
stack.push(new Guide(0, root));
while (stack.length !== 0) {
let curr = stack.pop();
if (curr.node === null) continue;
if (curr.operation === 1) {
console.log(curr.node.value);
}
// Put what to do next for the nodes in stack in reversed order
// Execution order is in reversed way
else {
stack.push(new Guide(0, curr.node.right));
stack.push(new Guide(0, curr.node.left));
stack.push(new Guide(1, curr.node));
}
}
}
let root = new TreeNode("A");
root.left = new TreeNode("B");
root.right = new TreeNode("C");
root.left.left = new TreeNode("D");
root.left.right = new TreeNode("E");
root.right.left = new TreeNode("F");
root.right.right = new TreeNode("G");
// dfs(root); //1, 2, 4, 5, 3, 6, 7
console.log(dfs_recursion(root));
// preorder_iteration(root);
/**
* Leetcode Fundamental: 11/5 Update
*/
/**
*
* @param {number} action
* @param {TreeNode} node
*/
function Command(action, node) {
return {
action, // 0: visit, 1: push to result
node
}
}
/**
* @param {TreeNode} root
* @return {number[]}
*/
var preorderTraversal = function(root) {
if (root === undefined || root === null) return [];
let result = [];
let stack = [Command(0, root)];
while (stack.length !== 0) {
let curr = stack.pop();
if (curr.action === 1) {
result.push(curr.node.val);
continue;
}
// Put next action for nodes in reversed order (1. Push curr node, 2. Visit left node, 3. Visit right node)
if (curr.node.right !== null) stack.push(Command(0, curr.node.right));
if (curr.node.left !== null) stack.push(Command(0, curr.node.left));
stack.push(Command(1, curr.node));
}
return result;
};