添加 0538.把二叉搜索树转换为累加树 Golang版本

添加 0538.把二叉搜索树转换为累加树 Golang版本
This commit is contained in:
X-shuffle
2021-06-19 16:30:36 +08:00
committed by GitHub
parent ed14cefdb1
commit d2e05f9b04

View File

@ -219,6 +219,26 @@ class Solution:
Go
> 弄一个sum暂存其和值
```go
//右中左
func bstToGst(root *TreeNode) *TreeNode {
var sum int
RightMLeft(root,&sum)
return root
}
func RightMLeft(root *TreeNode,sum *int) *TreeNode {
if root==nil{return nil}//终止条件,遇到空节点就返回
RightMLeft(root.Right,sum)//先遍历右边
temp:=*sum//暂存总和值
*sum+=root.Val//将总和值变更
root.Val+=temp//更新节点值
RightMLeft(root.Left,sum)//遍历左节点
return root
}
```
-----------------------