# 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: Optional[TreeNode]) -> Optional[TreeNode]:
_sum = 0
def inorderTravese_Reverse(root):
if not root:
return
else:
inorderTravese_Reverse(root.right)
nonlocal _sum
_sum += root.val
root.val = _sum
inorderTravese_Reverse(root.left)
inorderTravese_Reverse(root)
return root