1. Kth Smallest Element in a BST

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

Example 1:

  1. Input: root = [3,1,4,null,2], k = 1
  2. 3
  3. / \
  4. 1 4
  5. \
  6. 2
  7. Output: 1

Example 2:

  1. Input: root = [5,3,6,2,4,null,null,1], k = 3
  2. 5
  3. / \
  4. 3 6
  5. / \
  6. 2 4
  7. /
  8. 1
  9. Output: 3

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?

Constraints:

  • The number of elements of the BST is between 1 to 10^4.
  • You may assume k is always valid, 1 ≤ k ≤ BST's total elements.

解法1 利用BST中序序列有序的特点找第k次访问的节点

  1. class Solution {
  2. public:
  3. int kthSmallest(TreeNode* root, int k) {
  4. int order = 0, ans = INT_MAX;
  5. bool flag = false;
  6. in_order(root, order, k, flag, ans);
  7. return ans;
  8. }
  9. void in_order(TreeNode* root, int &order, int k, bool &flag, int &ans){
  10. if(flag)return;
  11. if(!root)return;
  12. in_order(root->left, order, k, flag, ans);
  13. order++;
  14. if(order == k){
  15. flag = true;
  16. ans = root->val;
  17. }
  18. in_order(root->right, order, k, flag, ans);
  19. }
  20. };

解法2 将BST修改成为中序的线索二叉树

【刷题-LeetCode】230. Kth Smallest Element in a BST的更多相关文章

  1. [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素

    题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...

  2. [LeetCode] 230. Kth Smallest Element in a BST 二叉搜索树中的第K小的元素

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

  3. 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. Not ...

  4. (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. Not ...

  5. [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. Not ...

  6. Java for LeetCode 230 Kth Smallest Element in a BST

    解题思路: 直接修改中序遍历函数即可,JAVA实现如下: int res = 0; int k = 0; public int kthSmallest(TreeNode root, int k) { ...

  7. LeetCode 230 Kth Smallest Element in a BST 二叉搜索树中的第K个元素

    1.非递归解法 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...

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

    返回排序二叉树第K小的数 还是用先序遍历,记录index和K进行比较 class Solution { public: void helper(TreeNode* node, int& idx ...

  9. 【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 k ...

随机推荐

  1. django的Cookie Session

    Cookie 初识cookie的设置和获取 示例 from django.shortcuts import render, redirect from django.shortcuts import ...

  2. 如何把myeclipse的工程导入到svn服务器上

    如何把myeclipse的工程导入到svn服务器上,按照如下步骤便可

  3. react Input 表单

    ​ input react 表单 input 密码框在谷歌浏览器下 会有黄色填充 官网的不太用,这个比较好用 type="password" autoComplete=" ...

  4. Linux启动、停止宝塔

    启动 /etc/init.d/bt start 停止 /etc/init.d/bt stop

  5. 【LeetCode】1631. 最小体力消耗路径 Path With Minimum Effort

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题思路 并查集 代码 刷题心得 欢迎加入组织 日期 题目地址:https ...

  6. 【LeetCode】1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1s Are at Least Length K Places Away

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 指针 日期 题目地址:https://leetcode ...

  7. 【LeetCode】1413. 逐步求和得到正数的最小值 Minimum Value to Get Positive Step by Step Sum

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 日期 题目地址:https://leetcode ...

  8. 【LeetCode】734. Sentence Similarity 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 只修改区间起终点 日期 题目地址:https://le ...

  9. 【九度OJ】题目1208:10进制 VS 2进制 解题报告

    [九度OJ]题目1208:10进制 VS 2进制 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1208 题目描述: 对于一 ...

  10. 【LeetCode】238. Product of Array Except Self 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 两次遍历 日期 题目地址:https://leetcode.c ...