-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary-tree-level-order-traversal.js
More file actions
145 lines (131 loc) · 3.35 KB
/
Copy pathbinary-tree-level-order-traversal.js
File metadata and controls
145 lines (131 loc) · 3.35 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
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
// https://leetcode.com/problems/binary-tree-level-order-traversal/
// Related Topics: Tree, BFS
// Difficulty: Easy
/*
Initial thoughts:
Using a BFS approach, we are going to queue the nodes for the next level
in a temporary queue and visit each node in the current queue while adding
their children to the temp queue.
When the current queue is empty, we are going to make the temp queue,
the current queue and start the process again untill there is no node left.
[JS Note]: Since JavaScript does not have a default Queue data structure and
using JS arrays as a queue will has an O(n) time complexity when shifting the head
we will create our own Queue abstract datatype based on a singly linked list.
Time complexity: O(n) where n is the number of nodes in the tree
Space complexity: O(n) where n is the number of nodes in the tree;
That is because we need to temporarily queue the nodes in a particular level
and since the last level of a binary tree holds approximately n/2 nodes, the
space complelxity is O(n)
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
const levelOrder = root => {
let currQueue = new Queue();
currQueue.enqueue(root);
let tempQueue = new Queue();
const result = [];
let tempRes = [];
while (currQueue.length > 0) {
const node = currQueue.dequeue();
if (node !== null) {
tempRes.push(node.val);
tempQueue.enqueue(node.left);
tempQueue.enqueue(node.right);
}
if (currQueue.length === 0) {
if (tempRes.length > 0) result.push(tempRes);
tempRes = [];
currQueue = tempQueue;
tempQueue = new Queue();
}
}
return result;
};
/*
A JavaScript implementation misusing JS arrays as Queue
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
const levelOrder = root => {
let currQueue = [root];
let tempQueue = [];
let result = [];
let tempRes = [];
while (currQueue.length > 0) {
const node = currQueue.shift();
if (node !== null) {
tempRes.push(node.val);
tempQueue.push(node.left);
tempQueue.push(node.right);
}
if (currQueue.length === 0) {
if (tempRes.length !== 0) result.push(tempRes);
currQueue = tempQueue;
tempQueue = [];
tempRes = [];
}
}
return result;
};
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class Queue {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
enqueue(data) {
const node = new Node(data);
if (this.head === null) {
this.head = node;
this.tail = node;
} else {
this.tail.next = node;
this.tail = node;
}
this.length++;
}
dequeue() {
if (this.head === null) return null;
else {
if (this.head === this.tail) {
const node = this.head;
this.head = this.tail = null;
this.length--;
return node.data;
} else {
const node = this.head;
this.head = this.head.next;
this.length--;
return node.data;
}
}
}
peek() {
if (this.head !== null) return this.head.data;
else return null;
}
}