This page looks best with JavaScript enabled

Leetcode 538 Convert BST to Greater Tree

Problem

https://leetcode.com/problems/convert-bst-to-greater-tree/

My Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# 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:
        self.sum = 0
        self.rvl(root)
        return root

    def rvl(self, node):
        if not node: return
        self.rvl(node.right)
        self.sum += node.val
        node.val = self.sum
        self.rvl(node.left)