My Swift Solution


  • 0
    S
    func mergeTrees(_ t1: TreeNode?, _ t2: TreeNode?) -> TreeNode? {
            var newNode: TreeNode? = nil
    
            if t1 == nil && t2 == nil {
                return newNode
            } else if t1 != nil && t2 != nil {
                newNode = TreeNode(t1!.val + t2!.val)
            } else {
                newNode = t1 == nil ? t2 : t1
            }
    
            newNode?.left = mergeTrees(t1?.left, t2?.left)
            newNode?.right = mergeTrees(t1?.right, t2?.right)
    
            return newNode
        }
    

Log in to reply
 

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