Trace the Anticlockwise Outline of a Binary Tree Diagram

Last updated: November 29, 2026

Quick Overview

To draw an outline around a binary tree diagram, return its boundary 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.

Google
Coding & Algorithms
Frontend Engineer
Google
November 29, 2026
Frontend Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


To draw an outline around a binary tree diagram, return its boundary 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 Google onsite question framed around drawing a hull around a rendered tree diagram. It has no clever trick; the interviewer is grading decomposition into three clean passes and the care taken to avoid duplicating the root, the corners, and leaves that also sit on a boundary.

What the Interviewer Expects
  • Decompose into left boundary, leaves, and reversed right boundary
  • Exclude leaves from both side boundaries so they appear once
  • Define the left boundary as preferring left children, falling back to right
  • Treat a single-node tree and a root with one child correctly
  • Run in O(n) time
Key Topics to Cover
binary trees
tree traversal
decomposition
edge-case handling
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
  • Return the clockwise outline instead.
  • Do it in a single traversal.
  • How would you compute the outline of an n-ary tree?
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: root. Output: list of values. The root comes first. The left boundary is the path from root.left following left children when they exist and right children otherwise, excluding the final leaf...

High-Level Design

Three passes keep each rule simple. First, walk down from root.left: while the current node is not a leaf, append it and move to its left child if present, otherwise its right child. Second, collect...


Submit Your Answer
Markdown supported