Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node {
- char val;
- Node left, right;
- }
- class SuperNode extends Node {
- public SuperNode(Node n, int depth) {
- this.val = n.val;
- this.left = n.left;
- this.right = n.right;
- this.depth = depth;
- }
- int depth;
- }
- public void printTree(Node root) {
- Queue<SuperNode> q = new ArrayDeque<SuperNode>();
- SuperNode sRoot = new SuperNode(root, 0);
- q.offer(sRoot);
- int currDepth = 0;
- while (!q.isEmpty()) {
- SuperNode sn = q.poll();
- if (sn.depth > currDepth) {
- System.out.println();
- ++currDepth;
- }
- SuperNode toAdd = new SuperNode(sn.left, sn.depth + 1);
- q.add(toAdd);
- toAdd = new SuperNode(sn.right, sn.depth + 1);
- q.add(toAdd);
- System.out.print(sn.val);
- }
- }
Add Comment
Please, Sign In to add comment