Swift solution - reverse InOrder traversal


  • 0
    class Solution {
        func convertBST(_ root: TreeNode?) -> TreeNode? {
            var root = root
            var sum = 0
            
            helper(&root, &sum)
            
            return root
        }
        
        func helper(_ root: inout TreeNode?, _ sum: inout Int) {
            guard let root = root else {
                return
            }
            
            helper(&root.right, &sum)
            
            root.val += sum
            sum = root.val
            
            helper(&root.left, &sum)
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.