Learn / Trees

Views and boundaries

Give every node a column: the root is 0, a left child is one less, a right child one more. The top view is the first node seen in each column during level order, the bottom view the last; vertical order sorts by column, then row, then value. The right view is the last node of each level, and the boundary is the left edge, the leaves, then the right edge upwards.

How to recognise it

  • The question asks what is visible from the top, bottom, left or right.
  • Nodes must be grouped by vertical line or diagonal.
  • The outline of the tree is wanted.

Columns

123456
Columns: 2 is at -1, 1 and 4 at 0, 3 and 5 at 1, 6 at 2. The top view is 2 1 3 6; the bottom view is 2 4 5 6.
ViewRule
topfirst node met in each column (level order)
bottomlast node met in each column (level order)
left / rightfirst / last node of each level
vertical orderall nodes of a column, sorted by row, then value
diagonald = number of left steps; group by d

What a node, a child, a leaf and a level are is on the Structures page.

The boundary

  1. The root.
  2. The left edge, top down, without leaves: keep going left, or right when there is no left.
  3. All leaves, left to right.
  4. The right edge, bottom up, without leaves.

Common mistakes

  • Using depth-first search for the top view without tracking depth.
  • Counting a leaf twice in the boundary, once as an edge node and once as a leaf.

Problems

0 of 6 solved

Check yourself

A left child's column is?

Why is level order used for the top view?

For the right view, which node of each level is shown?

In a diagonal traversal, which child stays on the same diagonal?