-
Notifications
You must be signed in to change notification settings - Fork 0
/
prune_tree.py
34 lines (26 loc) · 918 Bytes
/
prune_tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Definition for a binary tree node.
from typing import Optional
import json
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def toJSON(self):
return json.dumps(self, default=lambda o: o.__dict__,
sort_keys=True, indent=4)
class Solution:
def pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
return None if self.dfs(root) else root
def dfs(self, root: TreeNode) -> bool:
if not root:
return True
l, r = self.dfs(root.left), self.dfs(root.right)
if l:
root.left = None
if r:
root.right = None
return l and r and root.val == 0
if __name__ == "__main__":
root = TreeNode(1, None, TreeNode(0, TreeNode(0), TreeNode(1)))
print(Solution().pruneTree(root).toJSON())