30:00
Given the root of a binary tree whose node values are all 0 or 1, return the same tree where every subtree not containing a 1 has been removed.
root
0
1
A subtree of a node is the node itself plus all of its descendants.
The number of nodes in the tree is in the range [1,200][1, 200][1,200].
Node.val is either 0 or 1.
Node.val
Run code to see results.
Prune bottom-up: a node can only be judged after its children have been pruned. The tree has 4 nodes, 2 of them are 1.
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.