# Definition for a binary tree node.
# 用递归的思想来做题。
# 首先比较自身节点是否相同,然后比较节点左儿子,最后比较右儿子
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
# 当两个节点都为None意味着情况相同, 返回真
if p == None and q == None:return True
# 两个节点有一个为None返回False
if p == None and q!= None:return False
if p != None and q == None:return False
# 若值不同同样返回FALSE
if q.val != p.val : return False
# 最后自身递归调用,分别比较左儿子和右儿子。
return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)