Python Solution with Detailed Explanation

  • 0


    Same Tree

    Recursive Solution

    • Trees are same if the data in their roots match and the left & right subtrees match.
    • Time and Space Complexity: O(N)
    class Solution(object):
        def isSameTree(self, p, q):
            :type p: TreeNode
            :type q: TreeNode
            :rtype: bool
            if p is None and q is None:
                return True
            elif p is not None and q is not None:
                return (p.val == q.val) and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
                return False

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.