Boundary of Binary Tree
Boundary of Binary Tree is LeetCode 545 (Medium, premium). Given the root of a binary tree, return the values on its outline, going anticlockwise from the root:
- the root;
- the left boundary, top to bottom: start at
root.left, and from each node go to its left child if it has one, otherwise to its right child; - every leaf, left to right;
- the right boundary, bottom to top: the mirror of the left one, starting at
root.right.
Neither boundary includes a leaf, a boundary is empty when the root has no child on that side, the root itself is never a leaf, and no node is listed twice. The tree can have 10⁴ nodes in a single chain, so deep recursion is a risk.
- The number of nodes in the tree is in the range [1, 10⁴].
- -1000 <= Node.val <= 1000
Intuition
The boundary is three simple walks plus the root. The only real difficulty is that the pieces touch: the bottom of the left boundary is a leaf, and so is the bottom of the right boundary. Give each node one owner:
- Root: added first, on its own.
- Boundary walks: add only nodes that are not leaves.
- Leaf pass: adds every leaf, left to right.
With those rules each node lands in exactly one piece, so the pieces can simply be joined. The right boundary is collected top to bottom and reversed, because the anticlockwise outline climbs it from the bottom.
Outline questions split the drawing of a tree into pieces with clear owners. Binary Tree Right Side View, the left view, Top View and Bottom View of a binary tree all ask which nodes sit on one edge of the drawing; boundary traversal joins several of those edges into one loop.
Approach
Before reading on: in [1,2,3,4,5,6,null,null,null,7,8,9,10] node 4 is both the bottom of the left boundary and a leaf. Which of your passes should add it, and how does the other pass know to skip it?
Handle a lone root, then add the root
If the root has no children, return [root.val]. Otherwise start the answer with the root. The root is never treated as a leaf or as part of a boundary, so this is the only place it is added.
Walk the left boundary
Start at root.left. At each node, append its value unless it is a leaf, then move to its left child if it has one, otherwise to its right child. The walk ends at a leaf, which is left for the next pass.
Add every leaf, left to right
Start a stack with the root and pop until it is empty. A leaf is appended; any other node pushes its right child, then its left child, so the left side comes out first. An explicit stack keeps a 10⁴-deep chain from hitting the recursion limit.
Walk the right boundary and add it reversed
Start at root.right and mirror the left walk: keep non-leaf nodes, move right if possible, otherwise left. Collect them in a separate list and append it reversed, because the anticlockwise outline climbs the right side from the bottom.
Code in Python, C++ and Java
Common pitfalls
Adding leaves during the boundary walks
while node:
result.append(node.val)
node = node.left if node.left else node.rightwhile node:
if not is_leaf(node):
result.append(node.val)
node = node.left if node.left else node.rightEach walk ends at a leaf, and the leaf pass adds every leaf. Without the check, the bottom node of each boundary appears twice, for example 4 in [1,2,3,4].
Following only left children down the left boundary
node = node.left
node = node.left if node.left else node.right
The left boundary is the leftmost path, which goes through a right child when there is no left one. For [1,2,3,null,4,null,null,5,6] the boundary is [1, 2, 4, 5, 6, 3]; stopping at 2 loses 4.
Not returning early for a lone root
result = [root.val] # then the walks and the leaf pass
if is_leaf(root):
return [root.val]A root with no children looks like a leaf to the leaf pass, which adds it again. The answer for [1] would be [1, 1] instead of [1].
Edge cases
The left boundary is empty; the walk does not move into the right subtree. For [1,null,2,3,4] the answer is [1, 3, 4, 2].
Complexity
Boundary of Binary Tree FAQ
Is GFG Boundary Traversal of binary tree the same as LeetCode 545?
Yes. GeeksforGeeks asks for the same anticlockwise boundary traversal with the same rules: leaves are not part of either boundary, a missing side gives an empty boundary, and the right side is read bottom to top. LeetCode 545 is premium, so the GFG version is the free place to submit this code.
Is the left boundary the same as the left view?
No. The left view takes the first node on every level, which can come from the right subtree. The left boundary only follows the path from root.left downward and stops at a leaf. In [1,2,3,null,null,4] the left view is [1, 2, 4], but 2 is a leaf, so the left boundary is empty and 4 is added only as a leaf.