Skip to main content

Boundary of Binary Tree - Solution & Explanation

MediumPremiumFree on FleetCodeTreeDepth-First SearchBinary Tree20 min readAsked at: Amazon, Microsoft, Meta +7
Practice this problem

Problem Statement

The boundary of a binary tree is the concatenation of the root, the left boundary, the leaves ordered from left-to-right, and the reverse order of the right boundary.

The left boundary is the set of nodes defined by the following:

  • The root node's left child is in the left boundary. If the root does not have a left child, then the left boundary is empty.
  • If a node is in the left boundary and has a left child, then the left child is in the left boundary.
  • If a node is in the left boundary, has no left child, but has a right child, then the right child is in the left boundary.
  • The leftmost leaf is not in the left boundary.

The right boundary is similar to the left boundary, except it is the right side of the root's right subtree. Again, the leaf is not part of the right boundary, and the right boundary is empty if the root does not have a right child.

The leaves are nodes that do not have any children. For this problem, the root is not a leaf.

Given the root of a binary tree, return the values of its boundary.

 

Example 1:

Input: root = [1,null,2,3,4]
Output: [1,3,4,2]
Explanation:
- The left boundary is empty because the root does not have a left child.
- The right boundary follows the path starting from the root's right child 2 -> 4.
  4 is a leaf, so the right boundary is [2].
- The leaves from left to right are [3,4].
Concatenating everything results in [1] + [] + [3,4] + [2] = [1,3,4,2].

Example 2:

Input: root = [1,2,3,4,5,6,null,null,null,7,8,9,10]
Output: [1,2,4,7,8,9,10,6,3]
Explanation:
- The left boundary follows the path starting from the root's left child 2 -> 4.
  4 is a leaf, so the left boundary is [2].
- The right boundary follows the path starting from the root's right child 3 -> 6 -> 10.
  10 is a leaf, so the right boundary is [3,6], and in reverse order is [6,3].
- The leaves from left to right are [4,7,8,9,10].
Concatenating everything results in [1] + [2] + [4,7,8,9,10] + [6,3] = [1,2,4,7,8,9,10,6,3].

 

Constraints:

  • The number of nodes in the tree is in the range [1, 104].
  • -1000 <= Node.val <= 1000

Approach Overview

Problem Overview: Given a binary tree, return its boundary traversal in anti-clockwise order starting from the root. The boundary includes the left boundary, all leaf nodes, and the right boundary (added in reverse order) without duplicates.

Approach 1: DFS with Boundary Classification (O(n) time, O(h) space)

This approach performs a depth-first traversal while classifying each node as part of the left boundary, right boundary, or a leaf. The root is always included first. During DFS, you propagate two flags: isLeftBoundary and isRightBoundary. If a node is on the left boundary, append it before exploring children. If it is a leaf (no children), add it directly. If it is on the right boundary, append it after exploring children so the final order becomes reversed automatically. This technique ensures each node is processed exactly once, giving O(n) time complexity and O(h) recursion stack space where h is tree height. The traversal naturally fits problems involving depth-first search on a binary tree.

Approach 2: Separate Traversals for Left Boundary, Leaves, and Right Boundary (O(n) time, O(h) space)

This method splits the work into three clear steps. First, iterate down the left side of the tree and collect non-leaf nodes to build the left boundary. Second, run a DFS across the tree to collect all leaf nodes in left-to-right order. Third, traverse the right side of the tree and push non-leaf nodes to a temporary list, then reverse it before appending to the result. Each step touches nodes at most once, so the total runtime remains O(n). Space complexity is O(h) due to recursion during the leaf DFS and the temporary storage for the right boundary. The logic is straightforward and commonly used in tree traversal problems.

Recommended for interviews: The DFS boundary-classification approach is usually preferred. It processes the tree in a single traversal and demonstrates strong control over DFS state and traversal order. The three-step traversal approach is easier to reason about and still optimal, which makes it a good starting point during interviews before refining into the single-pass DFS solution.

Solution

First, if the tree has only one node, we directly return a list with the value of that node.

Otherwise, we can use depth-first search (DFS) to find the left boundary, leaf nodes, and right boundary of the binary tree.

Specifically, we can use a recursive function dfs to find these three parts. In the dfs function, we need to pass in a list nums, a node root, and an integer i, where nums is used to store the current part's node values, and root and i represent the current node and the type of the current part (left boundary, leaf nodes, or right boundary), respectively.

The function implementation is as follows:

  • If root is null, then directly return.
  • If i = 0, we need to find the left boundary. If root is not a leaf node, we add the value of root to nums. If root has a left child, we recursively call the dfs function, passing in nums, the left child of root, and i. Otherwise, we recursively call the dfs function, passing in nums, the right child of root, and i.
  • If i = 1, we need to find the leaf nodes. If root is a leaf node, we add the value of root to nums. Otherwise, we recursively call the dfs function, passing in nums, the left child of root and i, as well as nums, the right child of root and i.
  • If i = 2, we need to find the right boundary. If root is not a leaf node, we add the value of root to nums. If root has a right child, we recursively call the dfs function, passing in nums, the right child of root, and i. Otherwise, we recursively call the dfs function, passing in nums, the left child of root, and i.

We call the dfs function separately to find the left boundary, leaf nodes, and right boundary, and then concatenate these three parts to get the answer.

The time complexity is O(n) and the space complexity is O(n), where n is the number of nodes in the binary tree.

Code

Python

Java

C++

Go

TypeScript

JavaScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
DFS with Boundary ClassificationO(n)O(h)Best general solution. Processes the tree in one traversal while maintaining boundary state.
Separate Left Boundary, Leaves, Right Boundary TraversalsO(n)O(h)Good when clarity matters. Splits the boundary logic into simple steps.

Video Solution

LeetCode 545. Boundary of Binary TreeHappy Coding6,697 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Boundary of Binary Tree easy or hard?
Boundary of Binary Tree is generally classified as a medium difficulty problem. The traversal itself is straightforward, but handling duplicates between left boundary, leaves, and right boundary requires careful logic.
Boundary of Binary Tree Python/Java solution
Most implementations use DFS recursion in Python, Java, C++, or Go. The code tracks whether a node belongs to the left boundary, right boundary, or leaf set and appends nodes to a result list in the correct order during traversal.
How to solve Boundary of Binary Tree in O(n)?
Traverse the tree using DFS and collect nodes in three categories: left boundary, leaves, and right boundary. Add left boundary nodes before recursion, collect leaves when both children are null, and append right boundary nodes after recursion so they appear in reverse order. Each node is processed once, giving O(n) complexity.
What is the best approach for Boundary of Binary Tree?
The most efficient approach uses depth-first search while classifying nodes as left boundary, right boundary, or leaf. Each node is visited once and placed in the result based on its boundary role. This produces the correct anti-clockwise order in O(n) time and O(h) space where h is the tree height.
Is Boundary of Binary Tree asked at Google/Amazon/Meta?
Boundary traversal problems appear in interviews at large tech companies because they test understanding of tree traversal and edge-case handling. Variants of this question have been reported in interviews at companies like Amazon and Google, especially for roles focused on data structures.
What data structure is used in Boundary of Binary Tree?
The primary data structure is a binary tree combined with depth-first search traversal. The solution typically stores the boundary nodes in an array or list while the recursion stack manages traversal state.
What is the time complexity of Boundary of Binary Tree?
The optimal solution runs in O(n) time because every node in the binary tree is visited exactly once during traversal. Space complexity is O(h) due to the recursion stack used in DFS, where h represents the height of the tree.

Ready to solve this problem?

Practice Boundary of Binary Tree with our built-in code editor and test cases.

Practice on FleetCode