学习二叉搜索树的遍历:从leetcode530的错误回顾

530. 二叉搜索树的最小绝对差 - 力扣(LeetCode)

代码示例

正确代码:

class Solution:
    def __init__(self):
        self.res = int(sys.maxsize)
        self.pre = int(sys.maxsize)
    def inorder(self,root):
        if not root:
            return
        self.inorder(root.left)
        if self.pre != int(sys.maxsize):
            self.res = min(self.res, abs(self.pre - root.val))
        self.pre = root.val
        self.inorder(root.right)
    def getMinimumDifference(self, root):
        self.inorder(root)
        return self.res

错误代码:

class Solution:
    def __init__(self):
        self.res = int(sys.maxsize)
    def inorder(self,root,pre):
        if not root:
            return
        self.inorder(root.

你可能感兴趣的:(算法,学习,算法,leetcode)