Recursive Swift solution, clean code


  • 0
        func isSameTree(p: TreeNode?, _ q: TreeNode?) -> Bool {
        if p == nil && q == nil {
            return true
        } else if (p == nil && q != nil )
            || (p != nil && q == nil) {
            return false
        }
        
        let sameValue = p!.val == q!.val
        let sameLeft = isSameTree(p!.left, q!.left)
        let sameRight = isSameTree(p!.right, q!.right)
        
        return sameValue && sameLeft && sameRight
        }
    

Log in to reply
 

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