Binary Tree Pruning - Problem

Imagine you have a binary tree where each node contains either 0 or 1. Your task is to prune this tree by removing all subtrees that don't contain at least one 1.

Think of it like trimming a plant - you want to cut off all the branches that don't have any fruit (represented by 1s). A subtree is considered "fruitless" if it contains only 0s.

What you need to do:

  • Given the root of a binary tree with 0s and 1s
  • Remove every subtree that contains only 0s
  • Return the pruned tree
  • A subtree includes a node and all its descendants

Key insight: If a subtree has no 1s anywhere in it, the entire subtree should be removed!

Input & Output

example_1.py โ€” Basic Tree Pruning
$ Input: [1,null,0,0,1]
โ€บ Output: [1,null,0,null,1]
๐Ÿ’ก Note: The left subtree of the right child (which was [0,0,null]) contained only zeros and was pruned. The right child itself (0) was kept because its right subtree contains a 1.
example_2.py โ€” Root Pruning
$ Input: [1,0,1,0,0,0,1]
โ€บ Output: [1,null,1,null,1]
๐Ÿ’ก Note: The entire left subtree [0,0,0] contained only zeros and was pruned. From the right subtree, only the path leading to 1 was preserved.
example_3.py โ€” Complete Tree Removal
$ Input: [0,0,0]
โ€บ Output: []
๐Ÿ’ก Note: The entire tree contains only zeros, so everything gets pruned, resulting in an empty tree (null root).

Visualization

Tap to expand
Tree Pruning VisualizationStep 1: Original Tree100001Step 2: After Pruning101โœ—โœ—โœ—Post-Order Traversal Process:1. Visit left subtree โ†’ Process nodes [0,0] โ†’ Both are 0-leaves, remove them2. Parent node 0 now has no children โ†’ Remove it too3. Visit right subtree โ†’ Node 0 has child 1 โ†’ Keep both (path to 1)4. Root node 1 has right child โ†’ Keep root
Understanding the Visualization
1
Start at Leaves
Begin at the bottom of the tree, examining leaf nodes first
2
Remove Zero Leaves
Cut off any leaf nodes that contain 0 (no fruit)
3
Move Up Tree
After pruning children, examine parent nodes
4
Prune Empty Branches
If a node has value 0 and no children left, remove it too
5
Preserve Paths to 1s
Keep any branch that leads to a 1, even if it contains 0s along the way
Key Takeaway
๐ŸŽฏ Key Insight: Post-order traversal ensures we process children before parents, allowing optimal pruning decisions in a single pass through the tree.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Visit each node exactly once in post-order traversal

n
2n
โœ“ Linear Growth
Space Complexity
O(h)

Recursion stack depth equals tree height h, O(log n) for balanced tree, O(n) for skewed tree

n
2n
โœ“ Linear Space

Constraints

  • The number of nodes in the tree is in the range [1, 200]
  • Node.val is either 0 or 1
  • Key constraint: Only subtrees containing exclusively 0s should be pruned
Asked in
Facebook 25 Google 18 Amazon 12 Microsoft 8
94.5K Views
Medium Frequency
~15 min Avg. Time
2.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen