-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBinaryTreeLevelOrderTraversal.js
More file actions
100 lines (90 loc) · 2.65 KB
/
BinaryTreeLevelOrderTraversal.js
File metadata and controls
100 lines (90 loc) · 2.65 KB
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
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
// TC: O(N) SC: O(N)
var levelOrder = function(root) {
if (!root) return []; // Base case: Handle an empty tree
const result = []; // Array to store the level-by-level traversal result
const queue = [root]; // Initialize the queue with the root node
while (queue.length > 0) {
const size = queue.length; // Store the number of nodes in the current level
const list = []; // Array to store the nodes of the current level
for (let i = 0; i < size; i++) {
const node = queue.shift(); // Dequeue a node from the front
list.push(node.val); // Add the node's value to the current level's list
if (node.left) queue.push(node.left); // Enqueue the left child if it exists
if (node.right) queue.push(node.right); // Enqueue the right child if it exists
}
result.push(list); // Add the current level's list to the result array
}
return result;
};
// Alternative - Creating A Custom Queue
class Node {
constructor (val, next = null) {
this.val = val;
this.next = next;
this.size = 0;
}
}
class ListQueue {
constructor() {
this.head = null;
this.size = 0;
this.tail = null;
}
isEmpty() {
return this.size === 0;
}
insert(data) {
if(!this.head) {
this.head = new ListNode(data);
this.tail = this.head;
}
else {
this.tail.next = new ListNode(data);
this.tail = this.tail.next;
}
this.size++;
return this.size;
}
remove() {
if(this.isEmpty()) return null;
else {
const data = this.head.val;
this.head = this.head.next;
this.size--;
return data;
}
}
length() {
return this.size;
}
}
var levelOrder = function(root) {
if(!root) return [];
const queue = new ListQueue();
const result = [];
queue.insert(root);
while(!queue.isEmpty()) {
const size = queue.length();
const list = [];
for(let i=0; i<size; i++) {
const treeNode = queue.remove();
if(treeNode.left) queue.insert(treeNode.left);
if(treeNode.right) queue.insert(treeNode.right);
list.push(treeNode.val);
}
result.push(list);
}
return result;
};