#814
Binary Tree Pruning
pupil · 520 · lc medium +28 · verified · 72.5% accepted · 4,664 likes · top 82%
Description
Given the root of a binary tree, remove every subtree that does not contain a 1, and return the modified tree.
A subtree rooted at a given node consists of that node and all of its descendants.
Example 1:
Input: root = [1,null,0,0,1]
Output: [1,null,0,null,1]
Explanation:
Only the red nodes satisfy the property "every subtree not containing a 1".
The diagram on the right represents the answer.
Example 2:
Input: root = [1,0,1,0,0,0,1]
Output: [1,null,1,null,1]
Example 3:
Input: root = [1,1,0,1,1,0,1,0]
Output: [1,1,0,1,1,null,1]
Code
1
2
3
4
5
6
7
8
9