All problems
0546MediumTreeDepth-First SearchBinary Tree

Outline of a Printed Tree Diagram

Tracked in this browser only
Write code

Trains the technique from

LeetCode 545Boundary of Binary Tree

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A screen reader has to announce the labels printed around the outside of a binary tree diagram, tracing anticlockwise from the top.

The diagram arrives as root, a level-order list. Its first entry is the top node's label. After that, the list gives the two children of each non-null node already listed, in the order those nodes appear, left child first, using null where a child is absent. Trailing null entries are left off.

Call a node a tip when it has no children. Two chains matter:

  • The left rail is empty when the top node has no left child. Otherwise begin at the top node's left child and repeatedly move to the current node's left child, or to its right child when there is no left child, stopping the moment a tip is reached. The rail holds the labels of the nodes stepped through before that tip, so no tip is ever on the rail.
  • The right rail is built the same way from the top node's right child, except that at each step the right child is taken when it exists and the left child only otherwise.

Return a list holding, in this order: the top node's label; the left rail from top to bottom; every tip in the order met by a walk that always goes into a node's left child before its right child; and the right rail from bottom to top. A diagram of a single node returns a list holding that one label.

Examples

Example 1

Input
root = [8, 3, 10, null, 6, 11, 20, 4, 7, 12]
Output
[8, 3, 6, 4, 7, 12, 20, 10]

The top label 8 comes first. The left rail starts at 3, which has no left child, so it steps right to 6; 6 has the left child 4, a tip, so the rail is 3 then 6. The tips read left to right are 4, 7, 12 and 20. The right rail starts at 10 and steps right to the tip 20, so it holds 10 alone.

Example 2

Input
root = [5, 4, null, 3, null, 2]
Output
[5, 4, 3, 2]

This diagram leans entirely left. The left rail runs 4 then 3 and stops at the tip 2, which is the only tip. The top node has no right child, so the right rail is empty, giving 5, 4, 3, 2.

Example 3

Input
root = [9]
Output
[9]

A single node is both the top of the diagram and a tip, and the rule for a one-node diagram lists its label once.

Constraints

  • The number of nodes in the tree is in the range [1, 10^4].
  • -1000 <= Node.val <= 1000
  • root is a level-order list as described, so root[0] is never null.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def boundary_of_binary_tree(root: list[int | None]) -> list[int]:
Java
public List<Integer> boundaryOfBinaryTree(Integer[] root)
September 7
Apply