-1

I tried to test a leetcode solution on my local PC (WSL, python3) to have a better understanding of the problem

from https://leetcode.com/problems/same-tree/discuss/2594714/python-recurse-in-same-function I modified it as

from typing import Optional
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
        
        if not p and not q:
            return True

        elif (p and not q) or (q and not p):
            return False

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

print(Solution.isSameTree([1,2,3],[1,2,3]))        

I got (binary_tree.py is the name of the file for the above code)

Traceback (most recent call last):
  File "binary_tree.py", line 24, in <module>
    print(Solution.isSameTree([1,2,3],[1,2,3]))
TypeError: isSameTree() missing 1 required positional argument: 'q'

Seems I have a very basic issue in using python class. But, what is the problem and how to fix it?

Chungji
  • 59
  • 5

1 Answers1

2

TLDR

You need to instantiate the Solution class instance before calling the class function. i.e.,

solution = Solution()
tree1 = TreeNode(1, TreeNode(2), TreeNode(3))
tree2 = TreeNode(1, TreeNode(2), TreeNode(3))
print(solution.isSameTree(tree1, tree2))

Or, you should add @classmethod or @staticmethod decorator on the isSameTree function.

Explanation

The error stack told you that TypeError: isSameTree() missing 1 required positional argument: 'q'. In other words. isSameTree() needs three required arguments while you only pass two of them(self, p). The first list of your arguments is considered as a self.

By instantiating a Solution class instance, calling with it will pass self as an argument automatically. Or you can add @classmethod to pass cls automatically without instantiating a class instance. @staticmethod has similar effect but works differently. Check answers under this question for more information: Difference between @staticmethod and @classmethod

Also, passing lists into isSameTree doesn't work either. You should build TreeNodes by TreeNode class.

hide1nbush
  • 759
  • 2
  • 14
  • I used `solution = Solution() print(solution.isSameTree(([1,2,3],[1,2,3])))` and still got ```Traceback (most recent call last): File "binary_tree.py", line 25, in print(solution.isSameTree(([1,2,3],[1,2,3]))) TypeError: isSameTree() missing 1 required positional argument: 'q'``` :( – Chungji Sep 20 '22 at 03:26
  • Thanks. It led to `AttributeError: 'list' object has no attribute 'left'` :(, I tried to input as in leetcode ... – Chungji Sep 20 '22 at 03:39
  • @Chungji plz check the updated answer above. Leetcode's checker hides lots of details from you. – hide1nbush Sep 20 '22 at 03:40
  • Thanks a lot! Leetcode binary tree looks confusing indeed. – Chungji Sep 20 '22 at 03:44