Tree views, boundary and vertical order
35 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)Tree views show nodes visible from exterior angles. Column indexes organize nodes vertically across columns.
Left and right views with breadth-first search
Tree views imagine looking at a tree structure from the left or right side. A naive approach stores every level into full lists and picks the ends. That uses extra list memory when we only need boundary nodes.
Example 1 · Right Side View using Queue
import java.util.ArrayList;import java.util.LinkedList;import java.util.List;import java.util.Queue;
public class Main { static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } }
static List<Integer> rightSideView(TreeNode root) { List<Integer> result = new ArrayList<>(); if (root == null) return result;
Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root);
while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { TreeNode curr = queue.poll(); if (i == size - 1) { result.add(curr.val); } if (curr.left != null) queue.offer(curr.left); if (curr.right != null) queue.offer(curr.right); } } return result; }
public static void main(String[] args) { TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(3); root.left.right = new TreeNode(5); root.right.right = new TreeNode(4);
System.out.println(rightSideView(root)); }}[1, 3, 4]
What this code does
- Breadth-first search processes the binary tree one level at a time.
- The last node processed in each level loop represents the rightmost view.
- Child nodes are pushed to the queue to prepare the subsequent level.
A cleaner approach uses level-order breadth-first search directly. For each level, record the queue size before starting the loop. The node at index 0 provides the left side view. The node at index size - 1 provides the right side view. Trace a tree with root 1, left child 2 having child 5, and right child 3. Level 0 contains [1]. Level 1 contains [2, 3]. Level 2 contains [5]. Level 0 sees node 1. Level 1 sees node 3. Level 2 sees node 5 for the right view.
Top and bottom views with column coordinates
Vertical views assign a horizontal distance or column number to every node. The root starts at column 0. A left child gets column - 1 and a right child gets column + 1. Queue pairs of nodes and column numbers in level order.
Example 2 · Top View with Column Coordinates
import java.util.LinkedList;import java.util.Map;import java.util.Queue;import java.util.TreeMap;
public class Main { static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } }
static class Pair { TreeNode node; int col; Pair(TreeNode node, int col) { this.node = node; this.col = col; } }
static void printTopView(TreeNode root) { if (root == null) return; Map<Integer, Integer> topViewMap = new TreeMap<>(); Queue<Pair> queue = new LinkedList<>(); queue.offer(new Pair(root, 0));
while (!queue.isEmpty()) { Pair curr = queue.poll(); if (!topViewMap.containsKey(curr.col)) { topViewMap.put(curr.col, curr.node.val); } if (curr.node.left != null) queue.offer(new Pair(curr.node.left, curr.col - 1)); if (curr.node.right != null) queue.offer(new Pair(curr.node.right, curr.col + 1)); }
System.out.println(topViewMap.values()); }
public static void main(String[] args) { TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(3); root.left.right = new TreeNode(4); root.left.right.right = new TreeNode(5);
printTopView(root); }}[2, 1, 3]
What this code does
- Each node is paired with its horizontal column index in the queue.
- The first time a column number is reached, its value is stored.
- TreeMap keeps the column numbers sorted from left to right.
For the top view, record only the first node discovered at each column number. For the bottom view, overwrite the entry so the latest node remains stored. Vertical order traversal groups all nodes sharing each column into sorted lists. Using a sorted map ensures columns appear from leftmost to rightmost automatically.
Boundary traversal around edges and leaves
Boundary traversal visits exterior nodes counter-clockwise without duplicates. First add the root if it is not a leaf node. Next collect the left boundary from top to bottom, omitting leaf nodes.
Example 3 · Counter-Clockwise Boundary Traversal
import java.util.ArrayList;import java.util.List;
public class Main { static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } }
static boolean isLeaf(TreeNode node) { return node.left == null && node.right == null; }
static void addLeftBoundary(TreeNode root, List<Integer> res) { TreeNode curr = root.left; while (curr != null) { if (!isLeaf(curr)) res.add(curr.val); curr = (curr.left != null) ? curr.left : curr.right; } }
static void addLeaves(TreeNode root, List<Integer> res) { if (isLeaf(root)) { res.add(root.val); return; } if (root.left != null) addLeaves(root.left, res); if (root.right != null) addLeaves(root.right, res); }
static void addRightBoundary(TreeNode root, List<Integer> res) { TreeNode curr = root.right; List<Integer> temp = new ArrayList<>(); while (curr != null) { if (!isLeaf(curr)) temp.add(curr.val); curr = (curr.right != null) ? curr.right : curr.left; } for (int i = temp.size() - 1; i >= 0; i--) { res.add(temp.get(i)); } }
public static void main(String[] args) { TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(3); root.left.left = new TreeNode(4); root.left.right = new TreeNode(5); root.right.left = new TreeNode(6); root.right.right = new TreeNode(7);
List<Integer> boundary = new ArrayList<>(); boundary.add(root.val); addLeftBoundary(root, boundary); addLeaves(root, boundary); addRightBoundary(root, boundary);
System.out.println(boundary); }}[1, 2, 4, 5, 6, 7, 3]
What this code does
- The left boundary descends without including leaf nodes.
- Inorder leaves collection gathers all bottom leaves from left to right.
- The right boundary collects ascending nodes in reverse order.
Then collect all leaves from left to right using depth-first traversal. Finally collect the right boundary from bottom to top, omitting leaves and root. If the tree has only one node, return that single root node immediately. Handling leaf exclusions carefully prevents nodes from appearing twice in the boundary.
Next step
Check your understanding
10 quick questions