python class

whitehousechef·2024년 1월 27일

I was solving inverse binary tree https://leetcode.com/problems/invert-binary-tree/submissions/1158036195/

when I came across method not being defined when Im using that method for recursion.

class Solution:
    def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        if root==None:
            return root
        root.left,root.right=invertTree(root.right),invertTree(root.left)
        return root

So when u any methods within a class in python, you NEED to use self.method_name to refer to that method.

Not only that, any method in a given class needs to have a self keyword as the FIRST parameter in any method.

0개의 댓글