List a binary tree's boundary: root, left edge, leaves, then right edge from the bottom up

Quick Overview

Return the boundary of a binary tree as a list: the root, the left edge from top to bottom, every leaf from left to right, and the right edge from bottom to top. It tests careful tree traversal and the edge cases where a leaf ends an edge or the root is missing a subtree.

List a binary tree's boundary: root, left edge, leaves, then right edge from the bottom up

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

The boundary of a binary tree is the concatenation of the root, the left boundary, the leaves, and the right boundary, in that order. Given a binary tree, return the values of its boundary nodes. The left boundary is defined as follows. If the root does not have a left child, the left boundary is empty. Otherwise, start from the root's left child and keep going to the left child if it exists; if it does not, go to the right child. Repeat until you reach a leaf. Leaf nodes are not part of the left boundary. The right boundary is defined the same way on the other side: it goes down the right side of the root's right subtree, preferring the right child and falling back to the left child. It does not include leaf nodes either, and its nodes are added in reverse order (bottom to top). The leaves are all leaf nodes, collected from left to right. A node can appear only once in the boundary. The root is not a leaf, even if it has no children. ### Function Signature ```python def boundary_of_binary_tree(tree: list[int | None]) -> list[int]: ``` ### Rules - `tree` is the binary tree in level order. `tree[0]` is the root. After it, the entries give the left child and then the right child of each existing node, taking the nodes level by level from left to right, with `None` for a missing child. A missing node has no entries for its children, and trailing `None` values may be omitted. - A leaf is a node with no children. The root never counts as a leaf, so a single-node tree returns a one-element list holding the root's value. - The output order is fixed: the root, then the left boundary from top to bottom, then the leaves from left to right, then the right boundary from bottom to top. - Nodes are identified by position. Two nodes with equal values are both listed when both lie on the boundary. ### Constraints - `1 <= number of nodes <= 10^4` - `-1000 <= node value <= 1000` ### Examples ```text Input: tree = [1, None, 2, 3, 4] Output: [1, 3, 4, 2] 1 \ 2 / \ 3 4 ``` The root has no left child, so the left boundary is empty. The leaves are 3 and 4, and the right boundary walk visits 2 and stops at the leaf 4, so only 2 is added after the leaves. ```text Input: tree = [1, 2, 3, 4, 5, 6, None, None, None, 7, 8, 9, 10] Output: [1, 2, 4, 7, 8, 9, 10, 6, 3] 1 / \ 2 3 / \ / 4 5 6 / \ / \ 7 8 9 10 ``` The left boundary walk visits 2 and stops at the leaf 4, and the leaves from left to right are 4, 7, 8, 9, 10. The right boundary walk visits 3, moves to its left child 6 because 3 has no right child, and stops at the leaf 10, so 3 and 6 are added bottom-up as 6, 3. ```text Input: tree = [1, 2, None, None, 3, 4] Output: [1, 2, 3, 4] 1 / 2 \ 3 / 4 ``` Node 2 has no left child, so the left boundary walk continues to its right child 3 and then to the leaf 4, giving 2 and 3. The only leaf is 4, and the right boundary is empty because the root has no right child.

Overview: Return the boundary of a binary tree as a list: the root, the left edge from top to bottom, every leaf from left to right, and the right edge from bottom to top. It tests careful tree traversal and the edge cases where a leaf ends an edge or the root is missing a subtree.

General logo
General
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

The boundary of a binary tree is the concatenation of the root, the left boundary, the leaves, and the right boundary, in that order. Given a binary tree, return the values of its boundary nodes.

The left boundary is defined as follows. If the root does not have a left child, the left boundary is empty. Otherwise, start from the root's left child and keep going to the left child if it exists; if it does not, go to the right child. Repeat until you reach a leaf. Leaf nodes are not part of the left boundary.

The right boundary is defined the same way on the other side: it goes down the right side of the root's right subtree, preferring the right child and falling back to the left child. It does not include leaf nodes either, and its nodes are added in reverse order (bottom to top).

The leaves are all leaf nodes, collected from left to right. A node can appear only once in the boundary. The root is not a leaf, even if it has no children.

Function Signature

def boundary_of_binary_tree(tree: list[int | None]) -> list[int]:

Rules

  • tree is the binary tree in level order. tree[0] is the root. After it, the entries give the left child and then the right child of each existing node, taking the nodes level by level from left to right, with None for a missing child. A missing node has no entries for its children, and trailing None values may be omitted.
  • A leaf is a node with no children. The root never counts as a leaf, so a single-node tree returns a one-element list holding the root's value.
  • The output order is fixed: the root, then the left boundary from top to bottom, then the leaves from left to right, then the right boundary from bottom to top.
  • Nodes are identified by position. Two nodes with equal values are both listed when both lie on the boundary.

Constraints

  • 1 <= number of nodes <= 10^4
  • -1000 <= node value <= 1000

Examples

Input: tree = [1, None, 2, 3, 4]
Output: [1, 3, 4, 2]

    1
     \
      2
     / \
    3   4

The root has no left child, so the left boundary is empty. The leaves are 3 and 4, and the right boundary walk visits 2 and stops at the leaf 4, so only 2 is added after the leaves.

Input: tree = [1, 2, 3, 4, 5, 6, None, None, None, 7, 8, 9, 10]
Output: [1, 2, 4, 7, 8, 9, 10, 6, 3]

            1
         /     \
      2           3
    /   \       /
   4     5     6
        / \   / \
       7   8 9   10

The left boundary walk visits 2 and stops at the leaf 4, and the leaves from left to right are 4, 7, 8, 9, 10. The right boundary walk visits 3, moves to its left child 6 because 3 has no right child, and stops at the leaf 10, so 3 and 6 are added bottom-up as 6, 3.

Input: tree = [1, 2, None, None, 3, 4]
Output: [1, 2, 3, 4]

      1
     /
    2
     \
      3
     /
    4

Node 2 has no left child, so the left boundary walk continues to its right child 3 and then to the leaf 4, giving 2 and 3. The only leaf is 4, and the right boundary is empty because the root has no right child.