100. Same Tree
https://leetcode.com/problems/same-tree
Python
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if not p or not q:
if not p and not q:
return True
else:
return False
if p.val != q.val:
return False
return self.isSameTree(p.left, q.left) \
and self.isSameTree(p.right, q.right)
Go
func isSameTree(p *TreeNode, q *TreeNode) bool {
if p == nil || q == nil {
if p == nil && q == nil {
return true
} else {
return false
}
}
if p.Val != q.Val {
return false
}
return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
}