-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelWiseLinkedlist.java
More file actions
93 lines (87 loc) · 2.68 KB
/
LevelWiseLinkedlist.java
File metadata and controls
93 lines (87 loc) · 2.68 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
// Level wise linkedlist
// Send Feedback
// Given a binary tree, write code to create a separate linked list for each
// level. You need to return the array which contains head of each level linked
// list.
// Input format :
// The first line of input contains data of the nodes of the tree in level order
// form. The data of the nodes of the tree is separated by space. If any node
// does not have left or right child, take -1 in its place. Since -1 is used as
// an indication whether the left or right nodes exist, therefore, it will not
// be a part of the data of any node.
// Output format :
// Each level linked list is printed in new line (elements are separated by
// space).
// Constraints:
// Time Limit: 1 second
// Sample Input 1:
// 5 6 10 2 3 -1 -1 -1 -1 -1 9 -1 -1
// Sample Output 1:
// 5
// 6 10
// 2 3
// 9
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class Solution {
/*
* BinaryTreeNode class
*
* class BinaryTreeNode<T>
* {
* T data;
* BinaryTreeNode<T> left;
* BinaryTreeNode<T> right;
* public BinaryTreeNode(T data)
* {
* this.data = data;
* }
* }
*/
/*
* LinkedListNode Class
* class LinkedListNode<T>
* {
* T data;
* LinkedListNode<T> next;
* public LinkedListNode(T data)
* {
* this.data = data;
* }
* }
*/
public static ArrayList<LinkedListNode<Integer>> constructLinkedListForEachLevel(BinaryTreeNode<Integer> root) {
// Write your code here
ArrayList<LinkedListNode<Integer>> result = new ArrayList<>();
if (root == null) {
return result;
}
Queue<BinaryTreeNode<Integer>> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int levelSize = queue.size();
LinkedListNode<Integer> levelHead = null;
LinkedListNode<Integer> levelTail = null;
for (int i = 0; i < levelSize; i++) {
BinaryTreeNode<Integer> current = queue.poll();
LinkedListNode<Integer> newNode = new LinkedListNode<>(current.data);
if (levelHead == null) {
levelHead = newNode;
levelTail = newNode;
} else {
levelTail.next = newNode;
levelTail = levelTail.next;
}
if (current.left != null) {
queue.add(current.left);
}
if (current.right != null) {
queue.add(current.right);
}
}
result.add(levelHead);
}
return result;
}
}