<p>Given the <code>root</code> of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.</p>

<p>As a reminder, a <em>binary search tree</em> is a tree that satisfies these constraints:</p>

<ul>
	<li>The left subtree of a node contains only nodes with keys <strong>less than</strong> the node&#39;s key.</li>
	<li>The right subtree of a node contains only nodes with keys <strong>greater than</strong> the node&#39;s key.</li>
	<li>Both the left and right subtrees must also be binary search trees.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/05/02/tree.png" style="width: 500px; height: 341px;" />
<pre>
<strong>Input:</strong> root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
<strong>Output:</strong> [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> root = [0,null,1]
<strong>Output:</strong> [1,null,1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
	<li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
	<li>All the values in the tree are <strong>unique</strong>.</li>
	<li><code>root</code> is guaranteed to be a valid binary search tree.</li>
</ul>

<p>&nbsp;</p>
<p><strong>Note:</strong> This question is the same as 1038: <a href="https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/" target="_blank">https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/</a></p>

# 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

#2023/05/21
'''
对于每一个节点就是用它本身，加上他的右侧节点/根节点的和，所以发现遍历顺序是右中左，且需要一个全局变量记录累加和
'''
# class Solution:
#     def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
#         self.prev=0
#         self.helper(root)
#         return root
#     def helper(self,root):
#         if root==None:
#             return
#         self.helper(root.right)
#         root.val=root.val+self.prev
#         self.prev=root.val
#         self.helper(root.left)

# class Solution:
#     def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
#         if root==None:
#             return root
#         s=[]
#         prev=0
#         node=root
#         while s or node:
#             while node:
#                 s.append(node)
#                 node=node.right
#             node=s.pop()
#             node.val+=prev
#             prev=node.val
#             node=node.left
#         return root


#2023/08/04
# class Solution:
#     def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
#         if root==None:
#             return None
#         self.prev=0
#         self.helpFuc(root)
#         return root
#     def helpFuc(self,root):
#         if root==None:
#             return 
#         self.helpFuc(root.right)
#         root.val+=self.prev
#         self.prev=root.val
#         self.helpFuc(root.left)


#2023/08/04
# class Solution:
#     def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
#         if root==None:
#             return None
#         stack=[]
#         prev=0
#         curr=root
#         while stack or curr:
#             while curr:
#                 stack.append(curr)
#                 curr=curr.right
#             curr=stack.pop()
#             curr.val+=prev
#             prev=curr.val
#             curr=curr.left
#         return root



#2023/08/07
# class Solution:
#     def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
#         if root==None:
#             return root
#         self.prev=0
#         self.helpFuc(root)
#         return root

#     def helpFuc(self,root):
#         if root==None:
#             return 
#         self.helpFuc(root.right)
#         root.val+=self.prev
#         self.prev=root.val
#         self.helpFuc(root.left)


# class Solution:
#     def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
#         if root==None:
#             return root
#         stack=[]
#         prev=0
#         curr=root
#         while stack or curr:
#             while curr:
#                 stack.append(curr)
#                 curr=curr.right
#             curr=stack.pop()
#             curr.val+=prev
#             prev=curr.val
#             curr=curr.left
#         return root

#2024/06/01
'''
一棵树的每个点的值变成它加上它右边所有节点的值
可以先遍历到最右节点，然后一个一个把当前最右节点的和和当前值加起来成为新的最右节点的值
'''
# class Solution:
#     def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
#         if root==None:
#             return root
#         stack=[]
#         rightSum=0
#         curr=root
#         while stack or curr:
#             while curr:
#                 stack.append(curr)
#                 curr=curr.right
#             curr=stack.pop()
#             curr.val=curr.val+rightSum
#             rightSum=curr.val
#             curr=curr.left
#         return root


'''
dfs也是只要先遍历右边，用一个变量存当前右边和就可以了
'''
# class Solution:
#     def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
#         if root==None:
#             return root
#         self.rightSum=0
#         def helper(root):
#             if root==None:
#                 return
#             helper(root.right)
#             root.val=root.val+self.rightSum
#             self.rightSum=root.val
#             helper(root.left)
#         helper(root)
#         return root

#2024/09/16
'''
每一个数的数值变成它加上比他大的数
因为是BST，也就是说，应该先去最右边，然后把每个node的数一个一个累加上来

所以inorder,但先走右边，走右边的时候累加
'''
class Solution:
    def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        self.curr=0
        def dfs(root):
            if root==None:
                return
            dfs(root.right)
            root.val=root.val+self.curr
            self.curr=root.val
            dfs(root.left)
        dfs(root)
        return root
18600