알고리즘
[Swift] LeetCode - 1038. Binary Search Tree to Greater Sum Tree
고고
2022. 2. 17. 10:52
문제: https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/
Binary Search Tree to Greater Sum Tree - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
오른쪽 -> 가운데 -> 왼쪽 순서로 돌면 됩니다!
class Solution {
var value = 0
func bstToGst(_ root: TreeNode?) -> TreeNode? {
if root != nil {
bstToGst(root!.right)
value += root!.val
root!.val = value
bstToGst(root!.left)
}
return root
}
}