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小的数,不就是遍历到第k个数吗?所以中序遍历非常easy想到,例如以下:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
stack<TreeNode*> nodeStack;
TreeNode* tmp=root;
int kthMin = 0;
int kthValue;
while ((!nodeStack.empty() || tmp!=NULL) && kthMin!=k)
{
while (tmp != NULL)
{
nodeStack.push(tmp);
tmp = tmp->left;
}
tmp = nodeStack.top();
nodeStack.pop();
kthMin++;//对中序遍历稍加改动
kthValue = tmp->val;
tmp = tmp->right;
}
return kthValue;
}
};

另外。看了一下网友的解答,很巧妙。

他是先统计左子树上节点个数,假设节点个数小于k。则在右子树上找第k-n-1小的数,假设刚为k则就是当前节点,假设大于k,则继续在左子树上找第k小的数。

只是。每次递归都要统计一次节点个数,会不会导致复杂度添加?

int kthSmallest(TreeNode* root, int k) {
if (!root) return 0;
if (k==0) return root->val; int n=count_size(root->left);
if (k==n+1) return root->val; if (n>=k){
return kthSmallest(root->left, k);
}
if (n<k){
return kthSmallest(root->right, k-n-1);
} } int count_size(TreeNode* root){
if (!root) return 0;
return 1+count_size(root->left)+count_size(root->right); }

leetCode(46):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

    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 二叉搜索树中的第K小的元素

    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. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)

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

随机推荐

  1. fontend-githubs

    https://github.com/dypsilon/frontend-dev-bookmarks

  2. SQL语法精讲(包括建库、建表、建视图、查询、增加、删除、)

    SQL语法精讲(包括建库.建表.建视图.查询.增加.删除.修改) SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELE ...

  3. Linux网络驱动程序

    Linux 的网络系统主要是基于 BSD UNIX 的套接字机制. 在系统与驱动程序之间定义了数据结构 sk_buff 进行传输数据.系统支持对发送数据和接收数据缓存,提供流控机制并提供对多协议的支持 ...

  4. k8s 架构浅析

    文章目录 目录 Kubernetes 的电梯间演讲 Kubernetes 的核心层级对象 Kubernetes 的组件架构 Kubernetes 的组件通信协议/接口 Kubernetes 的分层架构 ...

  5. BZOJ 3323 splay维护序列

    就第三个操作比较新颖 转化成 在l前插一个点 把r和r+1合并 //By SiriusRen #include <cstdio> #include <cstring> #inc ...

  6. java实现sql批量插入参数

    背景: 需要更新一些不规范的时间格式,如将某个时间格式化为yy-MM-dd,实际上为 yy-MM-dd hh:mm:ss,并且需要提供回滚脚本. 例如:规范化时间的脚本如下: ,) WHERE tes ...

  7. 以SqlHelper为例论面向对象中封装的使用

    引言: 在使用面向对象方法编写的程序中,会有一些工具类,如Utility,xxHelper等. 比如1)操作数据库的过程,一般步骤都是:1.准备数据库地址.表名等信息:2.建立连接:3.准备要执行sq ...

  8. PHP关于注册注意的问题

    1.注意转义字符的问题 get_magic_quotes_gpc()开启时,所有的 ' (单引号), " (双引号), \(反斜线) and 空字符(null)会自动转为含有反斜线的溢出字符 ...

  9. MyEclipse如何设置自动提示?

    MyEclipse --> Preferences --> Java --> Editor --> Content Assist --> Enable auto acti ...

  10. TensorFlow+实战Google深度学习框架学习笔记(5)----神经网络训练步骤

    一.TensorFlow实战Google深度学习框架学习 1.步骤: 1.定义神经网络的结构和前向传播的输出结果. 2.定义损失函数以及选择反向传播优化的算法. 3.生成会话(session)并且在训 ...