Boundary of Binary Tree Problem
Description
LeetCode Problem 545.
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, 10^4]. -1000 <= Node.val <= 1000
Solution
Approach: Left Edge, Leaves, Right Edge
The boundary is made of four pieces that never overlap, so we can collect them one at a time and join them:
- The root. If the root has no children, the answer is just
[root.val]. (The problem says the root is not a leaf, so it must not be added a second time as one.) - The left boundary. Start at
root.leftand walk down: go to the left child when there is one, otherwise to the right child. Add each node we pass, but stop as soon as we reach a leaf: that leaf belongs to the leaves. - The leaves, from left to right: a depth-first traversal that visits the left subtree before the right one and records every node with no children.
- The right boundary. The mirror image of step 2: start at
root.right, prefer the right child, else the left, stop before the leaf. It is listed bottom-up, so reverse it before appending.
Starting the two edges at root.left and root.right (not at the root) is what keeps the root out of them, and stopping before a leaf is what keeps each leaf from appearing twice. If the root has no left child, the left boundary is simply empty; the left subtree is not replaced by nodes of the right one. Example 1 shows this: the boundary is [1] + [] + [3,4] + [2].
Complexity: O(n) time: the leaf traversal visits every node once and the two edges are single paths. O(n) space for the answer and O(h) for the recursion, where h is the height of the tree.
class Solution:
def boundaryOfBinaryTree(self, root: Optional[TreeNode]) -> List[int]:
def is_leaf(node):
return node.left is None and node.right is None
def add_leaves(node):
if is_leaf(node):
leaves.append(node.val)
return
if node.left:
add_leaves(node.left)
if node.right:
add_leaves(node.right)
if is_leaf(root):
return [root.val] # the root is not a leaf, but it is still the boundary
# left boundary: go left when possible, else right, and stop before the leaf
left = []
node = root.left
while node and not is_leaf(node):
left.append(node.val)
node = node.left if node.left else node.right
leaves = []
add_leaves(root)
# right boundary: the mirror image, read bottom-up at the end
right = []
node = root.right
while node and not is_leaf(node):
right.append(node.val)
node = node.right if node.right else node.left
return [root.val] + left + leaves + right[::-1]