【LeetCode】100. 相同的树

【LeetCode】100. 相同的树_第1张图片
【LeetCode】100. 相同的树_第2张图片

解题思路

python 代码

# 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:
        if p==None and q==None:
            return True
        if p!=None and q!=None and q.val==p.val:
            return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
        else:
            return False

你可能感兴趣的:(数据结构与算法)