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 小的元素。

一个深度遍历的应用。使用递归、或者借助栈都可以实现深度遍历。本文代码使用递归实现。

     void visit(TreeNode* node){

         if (node->left != NULL){
visit(node->left);
} if (cnt == ) {
return;
} cnt--;
if(cnt == ){
res = node->val;
return ;
} if(node->right != NULL){
visit(node->right);
}
} int cnt;
int res; int kthSmallest(TreeNode* root, int k) {
cnt = k;
if(root == NULL){
return ;
}
visit(root); return (cnt == ) ? res : ;
}

[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. Java for LeetCode 230 Kth Smallest Element in a BST

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

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

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

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

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

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

  9. 【刷题-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 ...

随机推荐

  1. 通知中心 NSNotificationCenter

    NSNotificationCenter 通知中心提供了一种在程序内广播信息的途径,一个NSNotificationCenter对象本质上是一个通知分发表(notification dispatch ...

  2. UCOS 中的中断处理

    最近遇到一个问题,当我在UCOS里调用系统延时"OSTimeDlyHMSM(0, 0, 0, 10)",程序进入硬件错误中断“HardFault_Handler”中. 我开始以为是 ...

  3. jquery插件--多行文本缩略

    1.webkit内核多行缩略样式 text-overflow:ellipsis; display:-webkit-box; -webkit-line-clamp:3; -webkit-box-orie ...

  4. WordPress程序流程分析

    index.php 统一入口文件 包含wp-blog-heaer.php 包含wp-load.php 包含wp-config.php 数据库.语言包配置等 包含wp-setting.php 对各种运行 ...

  5. Memcached 搭建过程

    原文链接:http://www.open-open.com/lib/view/open1324368235733.html 安装 memcached 服务端 yum -y install libeve ...

  6. 设置Cacti图形标题能显示中文

    1.查看系统是否带有中文字体包 # ls /usr/share/fonts/chinese 如没有则安装 # yum -y install fonts-chinese   2.设置cacti使用的rr ...

  7. C++拾遗(六)函数相关(1)

    返回值 C++规定返回值不能是 数组.但可以是其它任何类型(包括结构体和对象). 通常,函数将返回值复制到指定的CPU寄存器或内存单元中,然后调用函数调用该内存单元的值. 函数原型 参数列表中可以不包 ...

  8. [转]简述volatile

    volatile int i=10; int j = i; ... int k = i; volatile 告诉编译器i是随时可能发生变化的,每次使用它的时候必须从i的地址中读取,因而编译器生成的可执 ...

  9. 工作流activiti-02事物控制、流程引擎创建

    使用activiti中有个很重要的问题就是需要保证事物的控制 activiti使用的是mybatis作为orm技术 封装了一系列的操作数据库操作  这也就是大家调用的api 操作的数据库表都是acti ...

  10. keepalived + nginx

    本文主要介绍keepalived的安装,Nginx自行解决,也可以使用httpd.随便任何服务都可以... keepalived 官网http://www.keepalived.org/index.h ...