package pep.Day31;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;
public class Level_Order_Traversal_BinaryTree {
public static class Node {
int data;
Node left;
Node right;
Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
public static class Pair {
Node node;
int state;
Pair(Node node, int state) {
this.node = node;
this.state = state;
}
}
public static Node construct(Integer[] arr) {
Node root = new Node(arr[0], null, null);
Pair rtp = new Pair(root, 1);
Stack<Pair> st = new Stack<>();
st.push(rtp);
int idx = 0;
while (st.size() > 0) {
Pair top = st.peek();
if (top.state == 1) {
idx++;
if (arr[idx] != null) {
top.node.left = new Node(arr[idx], null, null);
Pair lp = new Pair(top.node.left, 1);
st.push(lp);
} else {
top.node.left = null;
}
top.state++;
} else if (top.state == 2) {
idx++;
if (arr[idx] != null) {
top.node.right = new Node(arr[idx], null, null);
Pair rp = new Pair(top.node.right, 1);
st.push(rp);
} else {
top.node.right = null;
}
top.state++;
} else {
st.pop();
}
}
return root;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Integer[] arr = new Integer[n];
String[] values = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
if (values[i].equals("n") == false) {
arr[i] = Integer.parseInt(values[i]);
} else {
arr[i] = null;
}
}
Node root = construct(arr);
levelOrderTraversal(root);
}
private static void levelOrderTraversal(Node root) {
Queue<Node> mainQueue = new ArrayDeque<>();
mainQueue.add(root);
while (mainQueue.size() > 0) {
int size = mainQueue.size();
for (int i = 0; i < size; i++) {
root = mainQueue.remove();
System.out.print(root.data + " ");
if (root.left != null)
mainQueue.add(root.left);
if (root.right != null)
mainQueue.add(root.right);
}
System.out.println();
}
}
}
No comments:
Post a Comment