-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path538. Convert BST to Greater Tree.py
48 lines (48 loc) · 1.61 KB
/
538. Convert BST to Greater 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
#ye mene kia inorder bana k har node ka prefix sum nikal k node k sath map kia or baad me firse traverse kara k value badal di, but this is not efficient way
#def convertBST(self, root: TreeNode) -> TreeNode:
# l=[]
# d={}
# self.inorder(root,l)
# #print(l)
# for i in range(len(l)):
# d[l[i]]=sum(l[i:])
# #print(d)
# self.modifier(root,d)
# #print(root)
# return root
#def modifier(self,root,d):
# if root:
# self.modifier(root.left,d)
# #print(root.val)
# root.val=d[root.val]
# self.modifier(root.right,d)
#def inorder(self,root,l):
# if root:
# self.inorder(root.left,l)
# l.append(root.val)
# self.inorder(root.right,l)
#ye seekha h, kaise single traverse me karna h ise
#using reverse inorder traversal (Right, Root, Left)
sum_val=0
def convertBST(self, root: TreeNode) -> TreeNode:
node=self.modifier(root)
return node
def modifier(self,root):
if root:
self.modifier(root.right)
self.sum_val+=root.val
root.val=self.sum_val
self.modifier(root.left)
return root