Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- class Node
- int data;
- Node left;
- Node right;
- */
- void topView(Node root) {
- if (root == null) return;
- Stack<Node> stack = new Stack();
- while (root != null) {
- stack.push(root);
- root = root.left;
- }
- while (!stack.isEmpty()) {
- root = stack.pop();
- System.out.print(root.data + " ");
- }
- root = root.right;
- while (root != null) {
- System.out.print(root.data + " ");
- root = root.right;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment