-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintLevelwise.java
More file actions
118 lines (100 loc) · 3.02 KB
/
PrintLevelwise.java
File metadata and controls
118 lines (100 loc) · 3.02 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
// Print Levelwise
// Send Feedback
// For a given a Binary Tree of type integer, print the complete information of
// every node, when traversed in a level-order fashion.
// To print the information of a node with data D, you need to follow the exact
// format :
// D:L:X,R:Y
// Where D is the data of a node present in the binary tree.
// X and Y are the values of the left(L) and right(R) child of the node.
// Print -1 if the child doesn't exist.
// Example:
// alt text
// For the above depicted Binary Tree, the level order travel will be printed as
// followed:
// 1:L:2,R:3
// 2:L:4,R:-1
// 3:L:5,R:6
// 4:L:-1,R:7
// 5:L:-1,R:-1
// 6:L:-1,R:-1
// 7:L:-1,R:-1
// Note: There is no space in between while printing the information for each
// node.
// Input Format:
// The first and the only line of input will contain the node data, all
// separated by a single space. Since -1 is used as an indication whether the
// left or right node data exist for root, it will not be a part of the node
// data.
// Output Format:
// Information of all the nodes in the Binary Tree will be printed on a
// different line where each node will follow a format of D:L:X,R:Y, without any
// spaces in between.
// Constraints:
// 1 <= N <= 10^5
// Where N is the total number of nodes in the binary tree.
// Time Limit: 1 sec
// Sample Input 1:
// 8 3 10 1 6 -1 14 -1 -1 4 7 13 -1 -1 -1 -1 -1 -1 -1
// Sample Output 1:
// 8:L:3,R:10
// 3:L:1,R:6
// 10:L:-1,R:14
// 1:L:-1,R:-1
// 6:L:4,R:7
// 14:L:13,R:-1
// 4:L:-1,R:-1
// 7:L:-1,R:-1
// 13:L:-1,R:-1
// Sample Input 2:
// 1 2 3 4 5 6 7 -1 -1 -1 -1 -1 -1 -1 -1
// Sample Output 2:
// 1:L:2,R:3
// 2:L:4,R:5
// 3:L:6,R:7
// 4:L:-1,R:-1
// 5:L:-1,R:-1
// 6:L:-1,R:-1
// 7:L:-1,R:-1
import java.util.LinkedList;
import java.util.Queue;
// Following is the structure used to represent the Binary Tree Node
class BinaryTreeNode<T> {
T data;
BinaryTreeNode<T> left;
BinaryTreeNode<T> right;
public BinaryTreeNode(T data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class Solution {
public static void printLevelWise(BinaryTreeNode<Integer> root) {
// Your code goes here
if (root == null) {
return;
}
Queue<BinaryTreeNode<Integer>> pendingChildren = new LinkedList<>();
pendingChildren.add(root);
while (!pendingChildren.isEmpty()) {
BinaryTreeNode<Integer> front = pendingChildren.poll();
if (front != null) {
System.out.print(front.data + ":");
}
if (front.left != null) {
System.out.print("L:" + front.left.data + ",");
pendingChildren.add(front.left);
} else {
System.out.print("L:" + "-1,");
}
if (front.right != null) {
System.out.print("R:" + front.right.data);
pendingChildren.add(front.right);
} else {
System.out.print("R:" + "-1");
}
System.out.println();
}
}
}