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?

Hint:

    1. Try to utilize the property of a BST.
    2. What if you could modify the BST node's structure?
    3. The optimal runtime complexity is O(height of BST).

题目意思:

  给定一棵二叉搜索树,找到第k小的元素

  注意:

    1、利用二叉搜索树的特性

    2、修改二叉搜索树的节点结构

    3、最优时间复杂度是0(树的高度)

解题思路:

方法一:

  二叉搜索树的特性:其中序遍历是有序的。故中序遍历访问,访问第k个元素即可。

方法二:

  利用分冶的方法。

  • 统计根节点左子树的节点个数cnt
  • 如果cnt+1 = k,则根节点即为第k个最小元素
  • 如果cnt+1 > k,则第k个最小元素在左子树,root = root->left;
  • 如果cnt+1 < k,则第k个最小元素在右子树,root = root->right;
  • 重复第一步

源代码:

方法一:

 class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
stack<TreeNode*> nodeStack;
if(root == NULL) return -;
while(true){
while(root){
nodeStack.push(root);
root = root->left;
}
TreeNode* node = nodeStack.top(); nodeStack.pop();
if(k == ) return node->val;
else root = node->right,k--;
}
}
};

方法二:

 class Solution {
public:
int calcNodeSize(TreeNode* root){
if( root == NULL) return ;
return + calcNodeSize(root->left) + calcNodeSize(root->right);
} int kthSmallest(TreeNode* root, int k) {
if(root == NULL) return ;
int cnt = calcNodeSize(root->left);
if(k == cnt + ) return root->val;
else if( k < cnt + ) return kthSmallest(root->left,k);
else return kthSmallest(root->right, k-cnt-);
}
};

  

Leetcode Kth Smallest Element in a BST的更多相关文章

  1. [LeetCode] 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 ...

  2. LeetCode Kth Smallest Element in a BST(数据结构)

    题意: 寻找一棵BST中的第k小的数. 思路: 递归比较方便. /** * Definition for a binary tree node. * struct TreeNode { * int v ...

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

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

  4. LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)

    230. 二叉搜索树中第K小的元素 230. Kth Smallest Element in a BST 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的 ...

  5. 【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 ...

  6. 【刷题-LeetCode】230. Kth Smallest Element in a BST

    Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...

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

  8. [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 ...

  9. 【LeetCode】230. Kth Smallest Element in a BST

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...

随机推荐

  1. awk命令详解

    搜索 纠正错误  添加实例 awk 文本和数据进行处理的编程语言 补充说明 awk 是一种编程语言,用于在linux/unix下对文本和数据进行处理.数据可以来自标准输入(stdin).一个或多个文件 ...

  2. java中面向对象的一些知识(二)

    一. 封装的讲解 什么是封装?为什么要封装?怎么实现封装? 封装的目的是为了提高程序的安全性.封装就是把不想让第三者看的属性,方法隐藏起来. 封装的实现方法是: 1.修改属性的可见性,限制访问. 2. ...

  3. 参数名ASCII码从小到大排序(字典序)

    /// <summary> /// Hashtable字典排序 /// </summary> /// <param name="parameters" ...

  4. python3 黑板客爬虫闯关游戏(三)

    第三关,先登录,再猜密码,这关难度较第二关大幅增加,要先去注册一个登录账号,然后打开F12,多登录几次,观察headers数据的变化 给出代码,里面注释很详细 import urllib.reques ...

  5. 《转载》myeclipse 上安装 Maven3

    本文转载自http://www.cnblogs.com/fancyzero/archive/2012/06/09/maven3.html 环境准备: JDK 1.6 Maven 3.0.4 myecl ...

  6. appium for iOS config

    appium-doctor: Running iOS Checks ✔ Xcode is installed at /Applications/Xcode.app/Contents/Developer ...

  7. 简单的sql server连接

    private string constring="data source=112.74.73.122;initial catalog=qzyData;user id=sa;password ...

  8. 女生学Web前端优势往往很明显

    学Web前端的女生不算少数,女生学习的成果也往往不比男生差,前端偏向设计.交互和产品方向,需要更加贴合用户,女生心思细腻,对页面细节把控更好,更具美感,对用户心理把握更准,这样的优势往往是男生所不具备 ...

  9. JAVASCRIPT常用API总结

    目录 元素查找 class操作 节点操作 属性操作 内容操作 css操作 位置大小 事件 DOM加载完毕 绑定上下文 去除空格 Ajax JSON处理 节点遍历 元素查找 // Node docume ...

  10. python之Socket网络编程

    什么是网络? 网络是由节点和连线构成,表示诸多对象及其相互联系.在数学上,网络是一种图,一般认为专指加权图.网络除了数学定义外,还有具体的物理含义,即网络是从某种相同类型的实际问题中抽象出来的模型.在 ...