Binary Tree Pruning (LeetCode 814): Solution and Explanation
Remove every subtree that contains no 1 from a binary tree of 0s and 1s. A post-order walk decides each node after its children are already pruned.
Intuition
A subtree contains a 1 when its root is 1 or when one of its child subtrees contains a 1. That second part is a question about the children, so the children have to be settled first.
Once both children are pruned, the rule for the node is simple. If it is a 0 and both children came back null, nothing below it holds a 1 and the whole subtree goes. Otherwise it stays.
Returning null for a removed node lets the parent drop the edge with a plain assignment, root.left = pruneTree(root.left). Cuts propagate upward naturally: a chain of zeros disappears one level at a time as the recursion unwinds.
Approach: Post-order DFS
- If
rootis null, return null. - Prune the left subtree and store the result in
root.left. - Prune the right subtree and store the result in
root.right. - If
root.val == 0and both children are now null, return null. - Otherwise return
root.
Step-by-step example
root = [1,null,0,0,1]
- Start at 1. Its left child is null, so recurse into the right child 0.
- At that 0, recurse left into the leaf 0. It is a 0 with no children, so it returns null and the edge is cut.
- Recurse right into the leaf 1. It is a 1, so it is kept.
- Back at the middle 0: its left is null but its right child 1 survived, so it is kept.
- Back at the root 1: it is a 1, so it is kept. The result is [1,null,0,null,1].
Code: Python, Java, C++
1class Solution:
2 def pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
3 if not root:
4 return None
5 root.left = self.pruneTree(root.left)
6 root.right = self.pruneTree(root.right)
7 if root.val == 0 and not root.left and not root.right:
8 return None
9 return root1class Solution {
2 public TreeNode pruneTree(TreeNode root) {
3 if (root == null) return null;
4 root.left = pruneTree(root.left);
5 root.right = pruneTree(root.right);
6 if (root.val == 0 && root.left == null && root.right == null) return null;
7 return root;
8 }
9}1class Solution {
2public:
3 TreeNode* pruneTree(TreeNode* root) {
4 if (!root) return nullptr;
5 root->left = pruneTree(root->left);
6 root->right = pruneTree(root->right);
7 if (root->val == 0 && !root->left && !root->right) return nullptr;
8 return root;
9 }
10};Time and space complexity
O(n)
Every node is visited once and does constant work after its children return.
O(h)
The recursion stack is as deep as the tree height h, which is n for a chain.
Other approaches
Contains-one helper (time O(n), space O(h))
Write a helper that returns whether a subtree contains a 1, and null out each child for which it returns false. It is the same post-order walk with a boolean return instead of a node return.
Top-down check per node (time O(n^2), space O(h))
For every node, scan its whole subtree for a 1 before deciding. Correct but repeats work, giving O(n^2) on a skewed tree.
Common mistakes
- Deciding a node before pruning its children. A 0 whose children are all zeros looks like it has children until those children are removed.
- Only removing leaves that are 0 in one pass. Without post-order, a chain of zeros needs repeated passes.
- Forgetting that the root itself can be removed, which makes the answer an empty tree.
- Not assigning the recursive result back to root.left and root.right, so cut subtrees stay attached.
Frequently asked questions
Why post-order and not pre-order?
Whether a node stays depends on whether anything below it stays. Pre-order would decide the node before that information exists.
Can the answer be empty?
Yes. If the tree has no 1 at all, the root is a 0 whose children are both pruned, so it returns null and the result is [].
Is a node with value 1 ever removed?
No. A node with value 1 is a subtree containing a 1 by itself, so it always stays, and so do all of its ancestors.