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?