-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary Tree Pruning LC-814.py
47 lines (33 loc) · 1.05 KB
/
Binary Tree Pruning LC-814.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
35
36
37
38
39
40
41
42
43
44
45
46
47
# 1st
# Normal approach using true and false, using a different function
class Solution:
def dfs(self, root):
if not root:
return True
if not root.left and not root.right and root.val==0:
return True
left=self.dfs(root.left)
right=self.dfs(root.right)
if left:
root.left = None
if right:
root.right = None
if left and right and root.val==0:
return True
return False
def pruneTree(self, root: TreeNode) -> TreeNode:
if self.dfs(root):
return None
return root
# 2nd
# Optimal approach, same complexity as above, still this is done in
# same fucntion, by returning the root and node itself
class Solution:
def pruneTree(self, root: TreeNode) -> TreeNode:
if not root:
return root
root.left=self.pruneTree(root.left)
root.right=self.pruneTree(root.right)
if root.val==0 and not root.left and not root.right:
return None
return root