Trains the technique from
LeetCode 545Boundary of Binary TreeThis 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:
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.
Example 1
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
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
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.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def boundary_of_binary_tree(root: list[int | None]) -> list[int]:public List<Integer> boundaryOfBinaryTree(Integer[] root)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.