mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 16:36:41 +08:00
37 lines
551 B
Go
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)
|
|
}
|