Swift Easy to understand solution


  • 0
    N

    class Solution {
    func mergeTrees(_ t1: TreeNode?, _ t2: TreeNode?) -> TreeNode? {
    var head: TreeNode?
    if (t1 != nil && t2 != nil ){
    head = TreeNode(t1!.val + t2!.val)
    head?.left = mergeTrees(t1!.left,t2!.left)
    head?.right = mergeTrees(t1!.right, t2!.right)
    } else if( t2==nil){
    return t1;
    } else {
    return t2;
    }
    return head;
    }
    }


Log in to reply
 

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