Swift solution - recursive, iterative + Stack


  • 0
    class Solution {
        func sumNumbers(_ root: TreeNode?) -> Int {
            return helper(root, 0)
        }
        
        private func helper(_ root: TreeNode?, _ sum: Int) -> Int {
            guard let root = root else {
                return 0
            }
            
            let val = sum * 10 + root.val
            
            if root.left == nil && root.right == nil {
                return val
            }
            
            return helper(root.left, val) + helper(root.right, val)
        }
        
        func sumNumbers_Stack(_ root: TreeNode?) -> Int {
            guard let root = root else {
                return 0
            }
            
            var stack = [(node: root, nodeSum:root.val)]
            var result = 0
            
            while !stack.isEmpty {
                let item = stack.removeLast()
                if item.node.left == nil && item.node.right == nil {
                    result += item.nodeSum
                }
                if let rightNode = item.node.right {
                    stack.append((node: rightNode, nodeSum: item.nodeSum * 10 + rightNode.val))
                }
                if let leftNode = item.node.left {
                    stack.append((node: leftNode, nodeSum: item.nodeSum * 10 + leftNode.val))
                }
            }
            
            return result
        }
    }
    

Log in to reply
 

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