-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Tree Level Order Traversal.java
More file actions
73 lines (58 loc) · 2.16 KB
/
Binary Tree Level Order Traversal.java
File metadata and controls
73 lines (58 loc) · 2.16 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
/*
https://leetcode.com/problems/binary-tree-level-order-traversal/
https://youtu.be/6ZnyEApgFYg?si=SqHadFk7WFyrsbZ8
Time Complexity: O(n) where n = # of nodes inside tree
Space Complexity: O(n) where n = # of nodes inside tree. All nodes are added to the result
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
List<List<Integer>> result = new ArrayList<>();
// Create a queue and add root to it
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
// Iterate queue
while (!queue.isEmpty()) {
// Get length of queue because ideally it will only hold all the elements on the same level at a time
int queueLen = queue.size();
// Create a Level array
List<Integer> level = new ArrayList<>();
// Iterate all the children nodes
for (int i = 0; i < queueLen; i++) {
// Pop element from queue
// NOTE: We haven't done it yet
TreeNode poppedElem = queue.remove();
// If popped node isn't empty, then add its value to level and its children to the queue
if (poppedElem != null) {
level.add(poppedElem.val);
// Some nodes don't have children, so if-condition will prevent level from having null values to begin with
queue.add(poppedElem.left);
queue.add(poppedElem.right);
}
}
// If Level isn't empty, then add it to the list
// IMPORTANT: Some nodes
if (!level.isEmpty()) {
result.add(level);
}
}
return result;
}
}