递归,遍历两颗树的节点判断值是否相等
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param root1 TreeNode类
# @param root2 TreeNode类
# @return bool布尔型
#
class Solution:
def isSameTree(self , root1: TreeNode, root2: TreeNode) -> bool:
# write code here
if not root1 and not root2: return True
if (not root1 and root2) or (root1 and not root2): return False
return root1.val == root2.val and self.isSameTree(root1.left, root2.left) and self.isSameTree(root1.right, root2.right)