Trees & BST · Pattern 4 of 12

Tree views: left, right, top, bottom and vertical order

Give every node a coordinate: its level, and a column (left child col - 1, right child col + 1). Each view keeps one node per level or per column; BFS order decides which one.

4 variations · 7 problems · 4 with animated walkthroughs

Watch the lesson

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

When to use it

  • What you see from the left, right, top or bottom
  • Group nodes by vertical column
  • The outline (boundary) of the tree

The template

Views
// right view: the last node of every BFS level (the first one gives the left view)
List<Integer> rightSideView(TreeNode root) {
    List<Integer> res = new ArrayList<>();
    Deque<TreeNode> q = new ArrayDeque<>();
    if (root != null) q.offer(root);
    while (!q.isEmpty()) {
        int size = q.size();
        for (int i = 0; i < size; i++) {
            TreeNode node = q.poll();
            if (i == size - 1) res.add(node.val);
            if (node.left != null) q.offer(node.left);
            if (node.right != null) q.offer(node.right);
        }
    }
    return res;
}

// top view: BFS with a column per node; the first node seen in a column wins
List<Integer> topView(TreeNode root) {
    TreeMap<Integer, Integer> first = new TreeMap<>();   // column -> value, sorted by column
    Deque<TreeNode> q = new ArrayDeque<>();
    Deque<Integer> cols = new ArrayDeque<>();
    if (root != null) { q.offer(root); cols.offer(0); }
    while (!q.isEmpty()) {
        TreeNode node = q.poll();
        int col = cols.poll();
        first.putIfAbsent(col, node.val);                // bottom view: put(), so the last one wins
        if (node.left != null) { q.offer(node.left); cols.offer(col - 1); }
        if (node.right != null) { q.offer(node.right); cols.offer(col + 1); }
    }
    return new ArrayList<>(first.values());
}

The variations

  1. 1One node per level

    BFS level by level: the last node of each level is the right view, the first is the left view. A DFS that visits right before left and records the first node at each new depth works too.

  2. 2One node per column

    BFS with a column number. Top view keeps the first node seen in each column, bottom view overwrites it with the last. Read the columns from left to right.

  3. 3All nodes by column

    Same coordinates, but keep every node in a list per column. BFS order breaks ties for the plain vertical order; the strict version sorts by (row, value) within a column.

  4. 4The outline of the tree

    Three parts in order: the left edge top-down (without leaves), every leaf left to right, the right edge bottom-up (without leaves). Add the root once.

Mistakes to avoid

  • Top view with DFS: the first node DFS reaches in a column is not always the highest one. Use BFS, or store the level and keep the smallest.
  • Vertical order traversal (the strict version): nodes in the same row and column must be sorted by value. Sort by (column, row, value).
  • Boundary: counting a leaf twice, once as part of the left edge and once as a leaf.
Next: BST basics: search, insert, delete and build →

Mark it done once you can write the template from memory and you have solved the worked examples yourself.