-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathReverseLevelOrderTraversal.js
66 lines (62 loc) · 1.8 KB
/
ReverseLevelOrderTraversal.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
/**
* @author Anirudh Sharma
*
* Given a binary tree of size N, find its reverse level order traversal i.e.,
* the traversal must begin from the last level.
*
* Constraints:
* 1 ≤ N ≤ 10^4
*/
const reverseLevelOrderTraversal = (root) => {
// Array to store result
const result = [];
// Special case
if (root === undefined) {
return result;
}
// Queue to store nodes of the tree
const nodes = [];
// Stack to store the nodes for reverse level order traversal
const reverseLOT = [];
// Add root to the queue
nodes.push(root);
// Loop until the queue is empty
while (nodes.length > 0) {
// Get the current node from the head
let current = nodes.shift();
// Add current node to the result
reverseLOT.unshift(current.data);
// Check if the right child exists
if (current.right !== null) {
nodes.push(current.right);
}
// Check if the left child exists
if (current.left !== null) {
nodes.push(current.left);
}
}
// Pop all the nodes from the stack and
// add them to the result
while (reverseLOT.length > 0) {
result.push(reverseLOT.shift());
}
return result;
};
function Node(data, left, right) {
this.data = (data === undefined ? 0 : data);
this.left = (left === undefined ? null : left);
this.right = (right === undefined ? null : right);
}
const main = () => {
let root = new Node(1);
root.left = new Node(3);
root.right = new Node(2);
console.log(reverseLevelOrderTraversal(root));
root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(60);
console.log(reverseLevelOrderTraversal(root));
};
main();