Group Binary Tree Leaves by Removal Round

Quick Overview

Practice grouping binary tree values by simultaneous leaf-removal rounds. Preserve repeated values and round order while allowing any order inside each group.

Group Binary Tree Leaves by Removal Round

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Group Binary Tree Leaves by Removal Round Given the root of a binary tree, repeatedly collect all current leaves, then remove those leaves. Continue until the tree is empty. Return the node values grouped by removal round, with the first round first. A leaf is a node with no children. All leaves in one round are collected before the next round begins. Values within the same round may appear in any order. ### Function `vector<vector<int>> findLeaves(TreeNode* root)` Use the supplied `TreeNode` objects and their `val`, `left`, and `right` fields. Do not replace the tree interface with an array-input function. ### Example 1 ```text Input tree: [8,4,9,null,6] Output: [[6,9],[4],[8]] ``` Initially, nodes `6` and `9` are leaves. Removing them makes node `4` a leaf. The root `8` is removed last. `[[9,6],[4],[8]]` is also valid. ### Example 2 ```text Input tree: [7] Output: [[7]] ``` ### Constraints - The tree has between `1` and `100` nodes. - `-100 <= Node.val <= 100`. ### Clarifications The level-order arrays describe trees for the examples; the function receives a root pointer. Different nodes may have equal values, so every node contributes one occurrence to its round. The rounds themselves must remain in chronological order. ### What a Strong Answer Covers - The meaning of simultaneous removal within a round. - Correct groups for uneven branches, a single node, and repeated values. - Why every node appears in its correct round exactly once. - Time and auxiliary-space bounds.

Overview: Practice grouping binary tree values by simultaneous leaf-removal rounds. Preserve repeated values and round order while allowing any order inside each group.

Google logo
Google
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Group Binary Tree Leaves by Removal Round

Given the root of a binary tree, repeatedly collect all current leaves, then remove those leaves. Continue until the tree is empty. Return the node values grouped by removal round, with the first round first.

A leaf is a node with no children. All leaves in one round are collected before the next round begins. Values within the same round may appear in any order.

Function

vector<vector<int>> findLeaves(TreeNode* root)

Use the supplied TreeNode objects and their val, left, and right fields. Do not replace the tree interface with an array-input function.

Example 1

Input tree: [8,4,9,null,6]
Output: [[6,9],[4],[8]]

Initially, nodes 6 and 9 are leaves. Removing them makes node 4 a leaf. The root 8 is removed last. [[9,6],[4],[8]] is also valid.

Example 2

Input tree: [7]
Output: [[7]]

Constraints

  • The tree has between 1 and 100 nodes.
  • -100 <= Node.val <= 100 .

Clarifications

The level-order arrays describe trees for the examples; the function receives a root pointer. Different nodes may have equal values, so every node contributes one occurrence to its round. The rounds themselves must remain in chronological order.

What a Strong Answer Covers Guidance

  • The meaning of simultaneous removal within a round.
  • Correct groups for uneven branches, a single node, and repeated values.
  • Why every node appears in its correct round exactly once.
  • Time and auxiliary-space bounds.