Facebook Interview Question
Testing / Quality AssurancesCountry: United States
Interview Type: Written Test
static class Node {
int v;
Node l;
Node r;
}
static void print(Node n) {
Deque<Node> stack = new ArrayDeque<>();
while (n != null) {
stack.push(n);
n = n.l;
}
doStack(stack);
}
static void doStack(Deque<Node> stack) {
if (stack.isEmpty()) {return;}
Node prev = stack.pop();
System.out.println(prev.v);
while (!stack.isEmpty()) {
Node n = stack.pop();
System.out.println(n.v);
print(prev.r);
prev = n;
}
print(prev.r);
}
- kadok October 28, 2018