Convert BST to Greater Tree

Convert BST to Greater Tree

Recursive Version - 88ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def __init__(self):
self.lSum = 0
def convertBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if not root:
return None
self.convertBST(root.right)
self.lSum += root.val
root.val = self.lSum
self.convertBST(root.left)
return root

leet code