Remove All Single-Child Nodes From a Binary Tree

Quick Overview

Remove every node that has exactly one child from a binary tree, letting that child take its place, and return the result as a level-order list. It tests post-order tree rewriting, collapsing chains of single-child nodes, replacing the root, and level-order serialization.

Remove All Single-Child Nodes From a Binary Tree

Company: Palo Alto Networks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a binary tree, remove all nodes that have **exactly one child**. When a single-child node is removed, its child replaces it in the tree. Leaf nodes (nodes with no children) should not be removed. Nodes with two children should also not be removed. Return the modified tree. ### Function Signature ```python def remove_single_child_nodes(tree: list[int | None]) -> list[int | None]: ... ``` ### Rules - The input tree is a level-order list. `None` marks a missing child, and a missing node has no entries for its own children, so `[1, None, 2]` is a root 1 whose only child is a right child 2. Trailing `None` values may be left out. - Return the modified tree in the same level-order encoding, with trailing `None` values removed. - Every node that has exactly one child in the input is removed, including the root and nodes whose parent is also removed. ### Constraints - `1 <= number of nodes <= 10^4` - `-10^5 <= node value <= 10^5` ### Examples ```text Input: tree = [1, 2, 3, None, None, 4, 5] Output: [1, 2, 3, None, None, 4, 5] ``` Node 2 is a leaf, and nodes 1 and 3 each have two children. No node has exactly one child, so the tree is unchanged. ```text Input: tree = [1, 2, 3, 4] Output: [1, 4, 3] ``` Node 2 has only a left child, 4. Node 2 is removed and node 4 takes its place as the left child of 1. ```text Input: tree = [1, 2, None, None, 3, 4, 5] Output: [3, 4, 5] ``` Node 1 has only a left child (2), and node 2 has only a right child (3). Both are removed, so node 3 with its children 4 and 5 becomes the root.

Overview: Remove every node that has exactly one child from a binary tree, letting that child take its place, and return the result as a level-order list. It tests post-order tree rewriting, collapsing chains of single-child nodes, replacing the root, and level-order serialization.

Palo Alto Networks logo
Palo Alto Networks
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a binary tree, remove all nodes that have exactly one child. When a single-child node is removed, its child replaces it in the tree.

Leaf nodes (nodes with no children) should not be removed. Nodes with two children should also not be removed.

Return the modified tree.

Function Signature

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

Rules

  • The input tree is a level-order list. None marks a missing child, and a missing node has no entries for its own children, so [1, None, 2] is a root 1 whose only child is a right child 2. Trailing None values may be left out.
  • Return the modified tree in the same level-order encoding, with trailing None values removed.
  • Every node that has exactly one child in the input is removed, including the root and nodes whose parent is also removed.

Constraints

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

Examples

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

Node 2 is a leaf, and nodes 1 and 3 each have two children. No node has exactly one child, so the tree is unchanged.

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

Node 2 has only a left child, 4. Node 2 is removed and node 4 takes its place as the left child of 1.

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

Node 1 has only a left child (2), and node 2 has only a right child (3). Both are removed, so node 3 with its children 4 and 5 becomes the root.