- 递归依据:两棵树相同位置节点的子树也必须相等
- 递归出口:两个节点全是None,相等;只有一个是None,不等;两个都不是None,但节点值不相等,不等。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
@classmethod
def isSameTree(self, p, q):
if p == None and q == None:
return True
elif p == None or q == None:
return False
elif p.val != q.val:
return False
""" :type p: TreeNode :type q: TreeNode :rtype: bool """
return (Solution.isSameTree(p.left, q.left) and Solution.isSameTree(p.right, q.right))