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

class Solution:
    def __init__(self):
        self.prevEle = TreeNode(-100000)
        self.firstEle = None
        self.secondEle = None

    def inOrder(self, root):
        if root is None:
            return
        self.inOrder(root.left)

        if not self.firstEle and root.val < self.prevEle.val:
            self.firstEle = self.prevEle
        if self.firstEle and root.val < self.prevEle.val:
            self.secondEle = root

        self.prevEle = root
        self.inOrder(root.right)

    def recoverTree(self, root):
        self.inOrder(root)
        if self.firstEle is None or self.secondEle is None:
            return
        self.firstEle.val, self.secondEle.val = self.secondEle.val, self.firstEle.val
        return

def main():
    root = TreeNode(3)
    root.left = TreeNode(9)
    root.right = TreeNode(20)
    root.right.left = TreeNode(15)
    root.right.right = TreeNode(7)
    print("max depth " + str(Solution.recoverTree(root)))

main()

I can't seem to run this and can't find online why. Why does it say recoverTree() is missing 1 required positional argument? I'm sending root in it.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Please read up on instantiating classes and calling methods. – quamrana Jun 06 '21 at 19:42
  • 1
    You are calling the method in the context of the class. You have to call in the context of an instance. – Klaus D. Jun 06 '21 at 19:42
  • Does this answer your question? [TypeError: Missing 1 required positional argument: 'self'](https://stackoverflow.com/questions/17534345/typeerror-missing-1-required-positional-argument-self) – mkrieger1 Jun 06 '21 at 19:44

1 Answers1

3

It seems you are calling a class method without an instance. Did you mean to write Solution().recoverTree(...)?

Arsenii Burov
  • 111
  • 2
  • 4