My Swift Solution


  • 0
    J
    func isSameTree(p: TreeNode?, _ q: TreeNode?) -> Bool {
            guard let pNode = p, let qNode = q else {
                if p == nil && q == nil {
                    return true
                } else {
                    return false
                }
            }
    
            let resultOfComparingValue = pNode.val == qNode.val
            let resultOfComparingLeftNode = self.isSameTree(pNode.left, qNode.left)
            let resultOfComparingRightNode = self.isSameTree(pNode.right, qNode.right)
    
            return resultOfComparingValue && resultOfComparingLeftNode && resultOfComparingRightNode
        }

Log in to reply
 

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