Right view

void rightView(Node root, List result) {
  Queue q = new LinkedList<>();
  q.offer(root);
  while (!q.isEmpty()) {
    int size = q.size();
    for (int i = 0; i < size; i++) {
      Node n = q.poll();
      if (i == size - 1) result.add(n.val);  // last of level
      if (n.left != null) q.offer(n.left);
      if (n.right != null) q.offer(n.right);
    }
  }
}
Advertisement

Left view

Add first node of each level (i == 0). Otherwise same.

Advertisement

Top view

// BFS with horizontal distance (col)
// First node at each col from top = top view
// TreeMap col -> value

Bottom view

Same as top view but overwrite: last node at each col from BFS = bottom view.