Skip to content

Instantly share code, notes, and snippets.

@phongngtuan
Created September 16, 2019 15:23
Show Gist options
  • Save phongngtuan/d989929a8df684da7cbbcb9f1a3f4d65 to your computer and use it in GitHub Desktop.
Save phongngtuan/d989929a8df684da7cbbcb9f1a3f4d65 to your computer and use it in GitHub Desktop.
binary-tree-pruning.py
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