您的位置:首页 > 编程语言 > Python开发

230. Kth Smallest Element in a BST

2017-06-17 21:14 197 查看
My solution with a global variable

class Solution(object):
def kthSmallest(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: int
"""
self.result = 0
_ = self._tree_count(root, k)
return self.result

def _tree_count(self, root, k):
if not root:
return 0
left = self._tree_count(root.left, k)
if left + 1 == k:
self.result = root.val
right = self._tree_count(root.right, k - left - 1)
return left + right + 1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python leetcode