funcconvertBST(root*TreeNode)*TreeNode{ifroot==nil{// Prevent out of bounds error when performing
// suffixSum[len(arr)-1] = arr[len(arr)-1] operation
returnroot}vararr[]intvarinorderfunc(root*TreeNode)inorder=func(root*TreeNode){ifroot==nil{return}inorder(root.Left)arr=append(arr,root.Val)inorder(root.Right)}inorder(root)// suffixSum[i] is the sum of arr[i...len(arr)-1]
suffixSum:=make([]int,len(arr))suffixSum[len(arr)-1]=arr[len(arr)-1]fori:=len(arr)-2;i>=0;i--{suffixSum[i]=suffixSum[i+1]+arr[i]}idx:=0varchangeValfunc(root*TreeNode)changeVal=func(root*TreeNode){ifroot==nil{return}changeVal(root.Left)root.Val=suffixSum[idx]idx++changeVal(root.Right)}changeVal(root)returnroot}