Loading

Same Tree

Given the roots of two binary trees p and q, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.

Solution:

class Solution:
    def isSameTree(self, p, q):
        """
        如果两个二叉树都为空,则两个二叉树相同;
        如果两个二叉树中有且只有一个为空,则两个二叉树一定不相同;
        如果两个二叉树都不为空,那么首先判断它们的根节点的值是否相同;
        若不相同则两个二叉树一定不同,若相同,再分别判断两个二叉树的左子树是否相同以及右子树是否相同;
        这是一个递归的过程,因此可以使用深度优先搜索,递归地判断两个二叉树是否相同。
        """
        if not p and not q:
            return True
        elif not p or not q:
            return False
        elif p.val != q.val:
            return False
        else:
            return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
posted @ 2023-06-28 10:25  Artwalker  阅读(3)  评论(0编辑  收藏  举报
Live2D