From d2e05f9b047bfd92faab571fa8276a776ce34b2f Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sat, 19 Jun 2021 16:30:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200538.=E6=8A=8A=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E8=BD=AC=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?=E7=B4=AF=E5=8A=A0=E6=A0=91=20Golang=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0538.把二叉搜索树转换为累加树 Golang版本 --- ...38.把二叉搜索树转换为累加树.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index 765e78d3..06a53ce7 100644 --- a/problems/0538.把二叉搜索树转换为累加树.md +++ b/problems/0538.把二叉搜索树转换为累加树.md @@ -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 +} +``` + -----------------------