简体   繁体   中英

What does the error mean here when it says it's missing one required argument when I am giving it one argument?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM