美文网首页
Same Tree (Easy)

Same Tree (Easy)

作者: 为什么我这么笨 | 来源:发表于2020-11-01 02:53 被阅读0次

Description:

Given two binary trees, 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.

Example 1:

Example 2:

Example 3:

Solution:

class Solution:

    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:

        if p == None:

            return q == None

        elif q == None:

            return False

        if p.val != q.val:

            return False

       

        return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)

Runtime: 32 ms, faster than 88.03% of Python3 online submissions for Same Tree.

Memory Usage: 12.9 MB, less than 99.32% of Python3 online submissions for Same Tree

相关文章

网友评论

      本文标题:Same Tree (Easy)

      本文链接:https://www.haomeiwen.com/subject/qzfyvktx.html