-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSolution.java
More file actions
34 lines (29 loc) · 774 Bytes
/
Solution.java
File metadata and controls
34 lines (29 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package Practice.DataStructures.Trees.TreeTopView;
import java.util.Stack;
public class Solution {
class Node {
int data;
Node left;
Node right;
}
void topView(Node root) {
if (root == null) return;
Stack<Node> lefties = new Stack<>();
Node left = root;
while (left != null) {
lefties.push(left);
left = left.left;
}
while (!lefties.isEmpty()) {
System.out.print(lefties.pop().data);
if (!lefties.isEmpty()) {
System.out.print(" ");
}
}
Node right = root.right;
while (right != null) {
System.out.print(" " + right.data);
right = right.right;
}
}
}