Files
LeetCode-Go/leetcode/0538.Convert-BST-to-Greater-Tree/538. Convert BST to Greater Tree.go
2021-02-11 03:53:43 +08:00

37 lines
551 B
Go

package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func convertBST(root *TreeNode) *TreeNode {
if root == nil {
return root
}
sum := 0
dfs538(root, &sum)
return root
}
func dfs538(root *TreeNode, sum *int) {
if root == nil {
return
}
dfs538(root.Right, sum)
root.Val += *sum
*sum = root.Val
dfs538(root.Left, sum)
}