01 · Question
Convert a BST so each node contains the sum of all keys greater than or equal to it.
02 · Solution
1def convertBST(root: Optional[TreeNode]) -> Optional[TreeNode]:2 total = 03 def dfs(node: Optional[TreeNode]) -> None:4 nonlocal total5 if not node:6 return7 dfs(node.right)8 total += node.val9 node.val = total10 dfs(node.left)11 dfs(root)12 return root