-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBinaryTreeInorderTraversal.js
More file actions
43 lines (37 loc) · 916 Bytes
/
Copy pathBinaryTreeInorderTraversal.js
File metadata and controls
43 lines (37 loc) · 916 Bytes
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
/**
* @param {TreeNode} root
* @return {number[]}
*/
// TC: O(N) SC: O(N) Recursion
const traversal = (root) => {
if(!root) return [];
const node = root;
const result = [];
const inorder = (root) => {
if(!root) return;
if(root.left) inorder(root.left);
result.push(root.val);
if(root.right) inorder(root.right);
}
inorder(node);
return result;
}
var inorderTraversal = function(root) {
return traversal(root);
};
//TC: O(N) SC: O(N) Iteration
var inorderTraversal = function(root) {
const result = [];
const stack = [];
let current = root;
while(current || stack.length > 0) {
while(current) {
stack.push(current);
current = current.left;
}
current = stack.pop();
result.push(current.val);
current = current.right;
}
return result;
};