KatInterview

Edge Silhouettes of a Level-Order Binary Tree

Trees & GraphsAuto-graded · 23 test cases
MediumAuto-gradedTrees & Graphsbinary-treebfslevel-order

Last updated

Build a binary tree from a level-order list with gaps and report the first and last node of every level, testing BFS level separation and level-order parsing.

You are given a binary tree encoded as a list in level-order (breadth-first) form. Each entry is either an integer value or None, which marks an absent child.

Encoding

  • The first entry is the root. If it is None, the tree is empty.
  • Going through the list in order, every real (non-None) node takes the next two unused entries as its left child and right child, in that order. The nodes are handled in the same order they appear in the list.
  • A None entry occupies a slot but is not a node, so it never receives children of its own.
  • Trailing None entries may be left out. If the list ends before a node's child slots are filled, the missing children are simply absent.

Task

Write a function that returns a list of two lists, [left_silhouette, right_silhouette]:

  • left_silhouette: for each depth from the root downward, the value of the leftmost node at that depth.
  • right_silhouette: for each depth from the root downward, the value of the rightmost node at that depth.

"Leftmost" and "rightmost" are determined by position within a level, not by which type of edge leads to the node. A right child can be the leftmost node of its level if nothing lies to its left at that depth.

Both lists are ordered from the root level to the deepest level. A level with exactly one node contributes that node's value to both lists.

If the tree is empty (the list is empty or its first entry is None), return [[], []].

Your solution should obtain both lists from a single traversal of the tree. Values may repeat, and the result depends only on node positions, not on value ordering, so the tree need not be a search tree.

Constraints

  • 0 <= len(level_order) <= 10^4
  • Each entry is an integer or None.
  • Values may repeat; do not use values to identify nodes.
  • The input is a valid level-order encoding as described above.
  • Return exactly [left_silhouette, right_silhouette].

Examples

Example 1

Input: level_edge_views([1, 2, 3, 4, 5, 6, 7])

Output: [[1, 2, 4], [1, 3, 7]]

Example 2

Input: level_edge_views([1, 2, 3, None, 4, None, 5])

Output: [[1, 2, 4], [1, 3, 5]]

Example 3

Input: level_edge_views([1, 1, 1, 1])

Output: [[1, 1, 1], [1, 1, 1]]

Hints

Hint 1

For a single level, which node do you see first when walking left to right, and which one last? Think about processing the tree one whole level at a time.

Hint 2

Before handling a level, record how many nodes are currently waiting in the queue. That count marks exactly where this level ends and the next begins, so no explicit depth counter is needed.

Hint 3

The input list is consumed in the same order BFS visits real nodes. Keep a pointer into the list and hand each dequeued node the next two slots as children, enqueueing only the non-None ones.