Trace the Anticlockwise Outline of a Binary Tree

Last updated: November 29, 2026

Quick Overview

Return the boundary of a binary tree in anticlockwise order starting at the root: the left boundary top-down, then all leaves left to right, then the right boundary bottom-up, with no node repeated.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Return the boundary of a binary tree in anticlockwise order starting at the root: the left boundary top-down, then all leaves left to right, then the right boundary bottom-up, with no node repeated.

A Meta onsite problem that is all about careful case analysis. The interviewer is checking that you define the three parts precisely, avoid double counting leaves and the root, and handle trees with a missing left or right side.

What the Interviewer Expects
  • Define left boundary nodes as non-leaves on the leftmost path
  • Collect leaves with a full traversal
  • Add the right boundary in reverse
  • Exclude leaves from the side boundaries to avoid duplicates
  • Handle a root with only one child
Key Topics to Cover
Binary trees
Tree traversal
Case analysis
Boundary traversal
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you print the clockwise boundary instead?
  • What if the root itself is a leaf?
  • Can you do it in a single traversal?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input is a binary tree root. The left boundary is the path from root.left following left children (or the right child when no left exists), excluding leaves. The right boundary is the mirror startin...

High-Level Design

Handle the root separately so it is never counted as a leaf or a boundary. Then:

  1. Walk from root.left: append each non-leaf, moving left if possible, else right.
  2. Collect leaves with a left-to-...

Submit Your Answer
Markdown supported