230. Kth Smallest Element in a BST
題目:
https://leetcode.com/problems/kth-smallest-element-in-a-bst/
難度:
Medium
跟昨天做的一道題類似吃沪,一上來就走取巧之路筹我。
InOrder排序缴允,輸出,當(dāng)然也完全可以用昨天的binary tree iterator,入stack,出stack,知道輸出第k位
class Solution(object):
def kthSmallest(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: int
"""
self.root = root
self.lst = []
self.inOrder(root)
return self.lst[k-1]
def inOrder(self, root):
if root == None:
return
self.inOrder(root.left)
self.lst.append(root.val)
self.inOrder(root.right)