Closed labuladong closed 1 year ago
# Aug 16
# LC 230 Kth smallest element in BST
# Just do a traversal and return from arr
class Solution:
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
return self.traverse(root)[k-1]
def traverse(self, r):
return self.traverse(r.left) + [r.val] + self.traverse(r.right) if r else []
# LC 700
# Search in a BST
# recusive solution is straightforward
class Solution:
def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if not root:
return None
if root.val == val:
return root
elif root.val < val:
return self.searchBST(root.right, val)
elif root.val > val:
return self.searchBST(root.left, val)
本期打卡已结算完成。报名最新一期的打卡活动 点这里