[Leetcode] Binary search/tree-230. Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.

Note: 
You may assume k is always valid, 1 ? k ? BST‘s total elements.

Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?

Solution:

1.  #1st naive idea is to use inorder traversal, because of the characteristics of binary search tree, when the kth number is visited,
#it is the kth smallest value time complexity o(k), worst time complexity o(n)

(1). use recursive way

 1  def bfsInorderRecursive(node, ansLst):
 2             if node:
 3                 bfsInorderRecursive(node.left, ansLst)
 4                 #print ("d: ", k, node.val)
 5                 ansLst.append(node.val)
 6                 bfsInorderRecursive(node.right, ansLst)
 7
 8         ansLst = []
 9         bfsInorderRecursive(root, ansLst)
10         return ansLst[k-1]

(2) use iterative way: use stack to add all left node into a stack, then   iterate to pop out to check the node‘s right node existing or not to insert into stack

 1       current = root
 2         stk = []            #stack
 3         cnt = 1
 4         done = 0
 5         while (not done):
 6             if current is not None:       #Reach the left most Node of the current Node
 7                 stk.append(current)
 8                 current = current.left
 9             else:
10                 if (len(stk) > 0):
11                     current = stk.pop(-1)
12                     if cnt == k:
13                        return current.val
14                     cnt += 1
15                     current = current.right
16                 else: done = 1
17         return 0
18         

reference:

http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/

follow up problem:

add another count property  for current node,  left subtree‘s node number as current node‘s count

we can keep track of count in a subtree of any node while building the tree.

reference:

http://www.geeksforgeeks.org/find-k-th-smallest-element-in-bst-order-statistics-in-bst/

时间: 2024-10-14 00:46:48

[Leetcode] Binary search/tree-230. Kth Smallest Element in a BST的相关文章

【LeetCode】230. Kth Smallest Element in a BST (2 solutions)

Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up:What if the BST is modified (insert/delete

[LeetCode] 230. Kth Smallest Element in a BST 解题思路

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 问题:找出二叉搜索树种第 k 小的元素. 一个深度遍历的应用.使用递归.或者借助栈都可以实现深度遍历.本文代码使用递归实现. 1 void visit(TreeNod

(medium)LeetCode 230.Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up:What if the BST is modified (insert/delete operations) often and you need

230. Kth Smallest Element in a BST

题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up:What if the BST is modified (insert/delete operations) often and you

230. Kth Smallest Element in a BST ——迭代本质:a=xx1 while some_condition: a=xx2

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up: What if the BST is modified (insert/delete operations) often and you nee

【LeetCode从零单刷】Kth Smallest Element in a BST

题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up: What if the BST is modified (insert/delete operations) often and you

[leedcode 230] Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up:What if the BST is modified (insert/delete operations) often and you need

[LC] 230. Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note:You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Example 1: Input: root = [3,1,4,null,2], k = 1 3 / 1 4   2 Output: 1 Example 2: Inpu

Java for LeetCode 230 Kth Smallest Element in a BST

解题思路: 直接修改中序遍历函数即可,JAVA实现如下: int res = 0; int k = 0; public int kthSmallest(TreeNode root, int k) { this.k = k; inorderTraversal(root); return res; } public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<

LeetCode 230. Kth Smallest Element in a BST 动态演示

返回排序二叉树第K小的数 还是用先序遍历,记录index和K进行比较 class Solution { public: void helper(TreeNode* node, int& idx, int k, int& res){ if(res!=INT_MAX) return; if(!node) return; //a(node) //lk("root",node) //dsp helper(node->left, idx, k, res); if(idx==