Prune every subtree of a binary tree that contains no 1

Quick Overview

Given a binary tree whose nodes hold only 0 or 1, remove every subtree that does not contain a 1 and return the root of the pruned tree. It tests recursive tree traversal, updating child links correctly, and handling a root that may itself be removed.

Prune every subtree of a binary tree that contains no 1

Company: Jane Street

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given the `root` of a binary tree, return the same tree where every **subtree** (of the given tree) not containing a `1` has been removed. A subtree of a node `node` is `node` plus every node that is a descendant of `node`. ### Function Signature ```python def prune_tree(root: Optional[TreeNode]) -> Optional[TreeNode]: ``` ### Rules - Each node is a `TreeNode` with an integer `val` and two children, `left` and `right`, each a `TreeNode` or `None`. - The examples write a tree as its level-order list, with `None` for a missing child and trailing `None` values dropped. The function receives and returns the root node, not the list. - If every node is removed, return `None`. The examples write that empty tree as `[]`. - You may modify the input tree in place. The result is compared by structure and values. ### Constraints - The number of nodes in the tree is in the range `[1, 200]`. - `Node.val` is either `0` or `1`. ### Examples ```text Input: root = [1, None, 0, 0, 1] Output: [1, None, 0, None, 1] ``` The root's right child is a `0` with two leaf children, a `0` and a `1`. Only the subtree made of the single `0` leaf is removed, since it does not contain a `1`. The middle `0` stays because its subtree still holds the `1` leaf. ```text Input: root = [1, 0, 1, 0, 0, 0, 1] Output: [1, None, 1, None, 1] ``` The root's left child and both of its children are `0`, so that whole subtree is removed. Under the root's right child, the `0` leaf goes and the `1` leaf stays. ```text Input: root = [0, 0, 0] Output: [] ``` Every node is `0`, so the whole tree is a subtree without a `1`. The function returns `None`.

Overview: Given a binary tree whose nodes hold only 0 or 1, remove every subtree that does not contain a 1 and return the root of the pruned tree. It tests recursive tree traversal, updating child links correctly, and handling a root that may itself be removed.

Jane Street logo
Jane Street
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given the root of a binary tree, return the same tree where every subtree (of the given tree) not containing a 1 has been removed.

A subtree of a node node is node plus every node that is a descendant of node.

Function Signature

def prune_tree(root: Optional[TreeNode]) -> Optional[TreeNode]:

Rules

  • Each node is a TreeNode with an integer val and two children, left and right , each a TreeNode or None .
  • The examples write a tree as its level-order list, with None for a missing child and trailing None values dropped. The function receives and returns the root node, not the list.
  • If every node is removed, return None . The examples write that empty tree as [] .
  • You may modify the input tree in place. The result is compared by structure and values.

Constraints

  • The number of nodes in the tree is in the range [1, 200] .
  • Node.val is either 0 or 1 .

Examples

Input: root = [1, None, 0, 0, 1]
Output: [1, None, 0, None, 1]

The root's right child is a 0 with two leaf children, a 0 and a 1. Only the subtree made of the single 0 leaf is removed, since it does not contain a 1. The middle 0 stays because its subtree still holds the 1 leaf.

Input: root = [1, 0, 1, 0, 0, 0, 1]
Output: [1, None, 1, None, 1]

The root's left child and both of its children are 0, so that whole subtree is removed. Under the root's right child, the 0 leaf goes and the 1 leaf stays.

Input: root = [0, 0, 0]
Output: []

Every node is 0, so the whole tree is a subtree without a 1. The function returns None.