2019-08-24 LeetCode 230. 二叉搜索树中第K小的元素

其实很快的,但是由于a=0的情况造成排查错误好久。
找不到错误的时候还是靠人工去推导

class Solution:
    def __init__(self):
        self.index=0

    def kthSmallest(self, root: TreeNode, k: int) -> int:
        if not root:return None
        a=self.kthSmallest(root.left,k)
        if a!=None:return a  # 错在这里,a=0的时候认为为空,不能写if a
        self.index+=1
        if self.index==k:return root.val
        b=self.kthSmallest(root.right,k)
        return b

你可能感兴趣的:(2019-08-24 LeetCode 230. 二叉搜索树中第K小的元素)