leetcode python3 相同的树

代码思路:采用递归思想,逐级判断树节点是否相等或为空,搜索完毕即可得到最终答案

class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        def cheak(p, q):
            if p == None and q == None:
                return True
            if p == None or q == None:
                return False
            if p.val != q.val:
                return False
            return cheak(p.left, q.left) and cheak(p.right, q.right)
        return cheak(p,q)
发布了30 篇原创文章 · 获赞 0 · 访问量 311

猜你喜欢

转载自blog.csdn.net/m0_37656366/article/details/104959707