Advertisement
zwliew

Untitled

Feb 8th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. class Solution:
  2. """
  3. @param root: the root
  4. @return: the same tree where every subtree (of the given tree) not containing a 1 has been removed
  5. """
  6. def pruneTree(self, root):
  7. def containsOne(node):
  8. if not node:
  9. return False
  10. return containsOne(node.left) or containsOne(node.right) or node.val == 1
  11. if not root:
  12. return None
  13. root.left = self.pruneTree(root.left)
  14. root.right = self.pruneTree(root.right)
  15. return root if containsOne(root) else None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement