1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func bstToGst(root *TreeNode) *TreeNode { var dfs func (node *TreeNode, sum int) int dfs = func (node *TreeNode, sum int) int { if node == nil { return sum } node.Val += dfs(node.Right, sum) return dfs(node.Left, node.Val) } dfs(root, 0) return root }
|