Class Solution

java.lang.Object
g0801_0900.s0814_binary_tree_pruning.Solution

public class Solution extends Object
814 - Binary Tree Pruning.<p>Medium</p> <p>Given the <code>root</code> of a binary tree, return <em>the same tree where every subtree (of the given tree) not containing a</em> <code>1</code> <em>has been removed</em>.</p> <p>A subtree of a node <code>node</code> is <code>node</code> plus every node that is a descendant of <code>node</code>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_2.png" alt="" /></p> <p><strong>Input:</strong> root = [1,null,0,0,1]</p> <p><strong>Output:</strong> [1,null,0,null,1]</p> <p><strong>Explanation:</strong> Only the red nodes satisfy the property &ldquo;every subtree not containing a 1&rdquo;. The diagram on the right represents the answer.</p> <p><strong>Example 2:</strong></p> <p><img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_1.png" alt="" /></p> <p><strong>Input:</strong> root = [1,0,1,0,0,0,1]</p> <p><strong>Output:</strong> [1,null,1,null,1]</p> <p><strong>Example 3:</strong></p> <p><img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/05/1028.png" alt="" /></p> <p><strong>Input:</strong> root = [1,1,0,1,1,0,1,0]</p> <p><strong>Output:</strong> [1,1,0,1,1,null,1]</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 200]</code>.</li> <li><code>Node.val</code> is either <code>0</code> or <code>1</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details