Created
September 16, 2019 15:23
-
-
Save phongngtuan/d989929a8df684da7cbbcb9f1a3f4d65 to your computer and use it in GitHub Desktop.
binary-tree-pruning.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def pruneTree(self, root: TreeNode) -> TreeNode: | |
if root == None: | |
return None | |
left = self.pruneTree(root.left) | |
right = self.pruneTree(root.right) | |
if root.val == 1 or left or right: # not prunable | |
root = TreeNode(root.val) | |
root.left = left | |
root.right = right | |
return root | |
else: | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment