Advertisement
_Alpha_

Zig-Zag bottom up level order

Jan 29th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. class Node{
  4.     int data;
  5.     Node left,right;
  6.     Node(int data){
  7.         this.data=data;
  8.         left=right=null;
  9.     }
  10. }
  11. public class Solution {
  12.     static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  13.     static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
  14.     static Queue<Node> q=new LinkedList<>();
  15.     static void reversequeue(){
  16.         Stack<Node> stack = new Stack<>();
  17.         while (!q.isEmpty()) {
  18.             stack.add(q.peek());
  19.             q.remove();
  20.         }
  21.         while (!stack.isEmpty()) {
  22.             q.add(stack.peek());
  23.             stack.pop();
  24.         }
  25.     }
  26.     static void zigZag(Node root)throws IOException{
  27.         q.add(root);
  28.         int i=1;
  29.         Stack<Integer> st=new Stack<>();
  30.         while(!q.isEmpty()){
  31.             int x=q.size();
  32.             if(i%2==0){
  33.                 while(x-->0){
  34.                     Node temp=q.poll();
  35.                     st.push(temp.data);
  36.                     if(temp.right!=null)
  37.                         q.add(temp.right);
  38.                     if(temp.left!=null)
  39.                         q.add(temp.left);
  40.                 }
  41.             }
  42.             else{
  43.                 reversequeue();
  44.                 while(x-->0){
  45.                     Node temp=q.poll();
  46.                     st.push(temp.data);
  47.                     if(temp.left!=null)
  48.                         q.add(temp.left);
  49.                     if(temp.right!=null)
  50.                         q.add(temp.right);
  51.                 }
  52.                 reversequeue();
  53.             }
  54.             i++;
  55.         }
  56.         while(st.size()>0){
  57.             bw.write(st.peek()+" ");
  58.             st.pop();
  59.         }
  60.     }
  61.     static Node insert(Node root,int data){
  62.         if(root==null)
  63.             return new Node(data);
  64.         else{
  65.             Node curr;
  66.             if(data<=root.data){
  67.                 curr=insert(root.left,data);
  68.                 root.left=curr;
  69.             }
  70.             else{
  71.                 curr=insert(root.right,data);
  72.                 root.right=curr;
  73.             }
  74.         }
  75.         return root;
  76.     }
  77.     public static void main(String[] args) throws IOException{
  78.         int t=Integer.parseInt(br.readLine());
  79.         while(t-->0){
  80.             int n=Integer.parseInt(br.readLine());
  81.             String[] st=br.readLine().split(" ");
  82.             Node root=null;
  83.             for(int i=0;i<n;i++){
  84.                 root=insert(root,Integer.parseInt(st[i]));
  85.             }
  86.             if(root==null){
  87.             bw.write("\n");
  88.                 t--;
  89.                 continue;
  90.             }
  91.             zigZag(root);
  92.             bw.write("\n");
  93.         }
  94.         bw.flush();
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement