daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jan 29th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    /*
  2.    
  3.     class Node
  4.        int data;
  5.        Node left;
  6.        Node right;
  7.    */
  8.    void topView(Node root) {
  9.       if (root == null) return;
  10.        
  11.       Stack<Node> stack = new Stack();
  12.       while (root != null) {
  13.           stack.push(root);
  14.           root = root.left;
  15.       }
  16.        
  17.       while (!stack.isEmpty()) {
  18.           root = stack.pop();
  19.           System.out.print(root.data + " ");
  20.       }
  21.      
  22.       root = root.right;
  23.       while (root != null) {
  24.           System.out.print(root.data + " ");
  25.           root = root.right;
  26.       }
  27.    }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top