-
Notifications
You must be signed in to change notification settings - Fork 0
/
Levelordertraversal.java
67 lines (62 loc) · 1.55 KB
/
Levelordertraversal.java
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
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
/*Question: Level order traversal.
Link: https://www.geeksforgeeks.org/problems/level-order-traversal/1
Example 1:
Input:
1
/ \
3 2
Output:
1 3 2
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output:
10 20 30 40 60
*/
class Node {
int data;
Node left, right;
public Node(int item) {
data = item;
left = right = null;
}
}
public class Levelordertraversal {
static ArrayList<Integer> levelOrder(Node root) {
/*This is the optimal approach having TC: O(n) and SC: O(n) {n: no of nodes, this code is using BFS} */
ArrayList<Integer> al = new ArrayList<>();
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
Node curr = q.poll();
al.add(curr.data);
if (curr.left != null) {
q.add(curr.left);
}
if (curr.right != null) {
q.add(curr.right);
}
}
return al;
}
public static void main(String[] args) {
Node 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);
ArrayList<Integer> levelOrderTraversal = levelOrder(root);
for (Integer value : levelOrderTraversal) {
System.out.print(value + " ");
}
}
}