Skip to content

Commit 3e7e9d6

Browse files
Create 538. Convert BST to Greater Tree
1 parent 8ede8f3 commit 3e7e9d6

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

538. Convert BST to Greater Tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
3+
total = 0
4+
node = root
5+
stack = []
6+
while stack or node:
7+
while node:
8+
stack.append(node)
9+
node = node.right
10+
11+
node = stack.pop()
12+
total += node.val
13+
node.val = total
14+
node = node.left
15+
return root
16+

0 commit comments

Comments
 (0)