Skip to content

Commit

Permalink
[Tree] Update solution for Same Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Feb 15, 2018
1 parent 518b7f6 commit 6232cf9
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions Tree/SameTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@

class SameTree {
func isSameTree(p: TreeNode?, _ q: TreeNode?) -> Bool {
if p == nil && q == nil {
return true
guard let p = p else {
return q == nil
}
if p == nil || q == nil || p!.val != q!.val {
guard let q = q else {
return p == nil
}

if p.val != q.val {
return false
}
return isSameTree(p!.left, q!.left) && isSameTree(p!.right, q!.right)

return isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
}
}

0 comments on commit 6232cf9

Please sign in to comment.