-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostOrder.js
36 lines (31 loc) · 891 Bytes
/
postOrder.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
// inorder is a depth first approach
class Node {
constructor(val,left,right) {
this.val = (val === undefined ? null: val);
this.left = (val === undefined ? null: left);
this.right = (val === undefined ? null: right);
}
}
var postorderTraversal = function(root) {
return root ? [...postorderTraversal(root.left), ...postorderTraversal(root.right),root.val]:[];
};
/*
1
2 3
4 5 6 7
*/
const main = () => {
/*root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(6);
root.right.right = new Node(7);*/
root = new Node(1);
root.right = new Node(2);
root.right.left = new Node(3);
console.log(inOrder(root));
// console.log(root);
}
main();