Guest User

Untitled

a guest
Apr 23rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. class Node {
  2.     char val;
  3.     Node left, right;
  4. }
  5.  
  6. class SuperNode extends Node {
  7.     public SuperNode(Node n, int depth) {
  8.         this.val = n.val;
  9.         this.left = n.left;
  10.         this.right = n.right;
  11.         this.depth = depth;
  12.     }
  13.     int depth;
  14. }
  15.  
  16. public void printTree(Node root) {
  17.     Queue<SuperNode> q = new ArrayDeque<SuperNode>();
  18.     SuperNode sRoot = new SuperNode(root, 0);
  19.     q.offer(sRoot);
  20.     int currDepth = 0;
  21.     while (!q.isEmpty()) {
  22.         SuperNode sn = q.poll();
  23.         if (sn.depth > currDepth) {
  24.             System.out.println();
  25.             ++currDepth;
  26.         }
  27.        
  28.         SuperNode toAdd = new SuperNode(sn.left, sn.depth + 1);
  29.         q.add(toAdd);
  30.         toAdd = new SuperNode(sn.right, sn.depth + 1);
  31.         q.add(toAdd);
  32.         System.out.print(sn.val);
  33.     }
  34. }
Add Comment
Please, Sign In to add comment