-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintTreeLevel-wise.java
More file actions
73 lines (64 loc) · 1.9 KB
/
PrintTreeLevel-wise.java
File metadata and controls
73 lines (64 loc) · 1.9 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
// Print Tree Level-wise
// Send Feedback
// Given a generic tree, print the input tree in level wise order. That is,
// print the elements at same level in one line (separated by space). Print
// different levels in different lines.
// The output for the above tree should be
// 10
// 20 30 40
// 40 50
// Input format :
// Elements in level order form separated by space (as per done in class). Order
// is -
// Root_data, n (No_Of_Child_Of_Root), n children, and so on for every element
// Output Format :
// Level wise print
// Sample Input :
// 10 3 20 30 40 2 40 50 0 0 0 0
// Sample Output :
// 10
// 20 30 40
// 40 50
import java.util.LinkedList;
import java.util.Queue;
public class Solution {
/*
* TreeNode structure
*
* class TreeNode<T> {
* T data;
* ArrayList<TreeNode<T>> children;
*
* TreeNode(T data){
* this.data = data;
* children = new ArrayList<TreeNode<T>>();
* }
* }
*/
public static void printLevelWise(TreeNode<Integer> root) {
/*
* Your class should be named Solution
* Don't write main().
* Don't read input, it is passed as function argument.
* Print output and don't return it.
* Taking input is handled automatically.
*/
if (root == null) {
return;
}
Queue<TreeNode<Integer>> queue = new LinkedList<>();
queue.add(root);
System.out.println(root.data);
while (!queue.isEmpty()) {
int levelSize = queue.size();
for (int i = 0; i < levelSize; i++) {
TreeNode<Integer> current = queue.poll();
for (int j = 0; j < current.children.size(); j++) {
System.out.print(current.children.get(j).data + " ");
queue.add(current.children.get(j));
}
}
System.out.println();
}
}
}