Same Tree (Easy)

2020-11-01  本文已影响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

上一篇 下一篇

猜你喜欢

热点阅读