mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
添加 0538.把二叉搜索树转换为累加树 Golang版本
添加 0538.把二叉搜索树转换为累加树 Golang版本
This commit is contained in:
@ -219,6 +219,26 @@ class Solution:
|
|||||||
Go:
|
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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
Reference in New Issue
Block a user