-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay-021.py
37 lines (31 loc) · 912 Bytes
/
Day-021.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
"""day 21
https://leetcode.com/problemset/all/?topicSlugs=binary-search-tree
"""
#https://leetcode.com/problems/convert-bst-to-greater-tree/submissions/
# 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:
def convertBST(self, root: TreeNode) -> TreeNode:
ans=TreeNode()
change=ans
stack=[root]
s=0
a=root
while stack and a:
while a.right:
stack.append(a.right)
a=a.right
a=stack.pop()
print(s,end='|')
s+=a.val
a.val=s
if a.left:
stack.append(a.left)
a=a.left
else:
a=TreeNode()
return root