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. php缓存小技巧

    1.缓存数组到文件: <?php $arr = array(2,3,5,76,7,8,22); $data = "<?php\nreturn ".var_export( ...

  2. 苹果Swift编程语言新手教程【中国版】

    Swift代码语言教程:在刚刚过去的WWDC2014大会上,苹果公司新公布了一种编程语言Swift.据悉.Swift语言继承了C语言以及Objective-C的特性,且克服了C语言的兼容性问题.对于广 ...

  3. Cocos2dx 多线程

    多-threaded负荷plist特征.获取知识的必要性: 1.多线程开启:pthread 2.怎样在线程中载入plist 一.多线程开启 当我们想在程序中开多线程中.第一想到的是cocos2d-x有 ...

  4. Linux命令之查找

    在Linux中,有非常多方法能够做到这一点.国外站点LinuxHaxor总结了五条命令,你能够看看自己知道几条.大多数程序猿,可能常常使用当中的2到3条,对这5条命令都非常熟悉的人应该是不多的. 1. ...

  5. linux mysql 数据目录文件夹移动及所遇到的问题

    一 .如果是fedora下用rpm包安装的mysql,修改方法如下: 如果这里说的不够清楚,可以到http://www.vipkj.net/post-839.html给我留言 MySQL默认的数据文件 ...

  6. C#正则怎么判断字符串中是否有汉字

    Regex r = new Regex(".*[\\u4e00-\\u9faf].*");r.IsMatch(username)

  7. IIS配置

    IIS配置文档: 1.安装IIS.控制面板→程序→打开关闭Windows功能,Web管理服务和万维网服务都勾上. 2.部署网站:ASP.Net项目的发布:项目中点右键“发布”,选择“文件系统”,发布到 ...

  8. java中的异常机制(编译时异常)

    / * 1 异常机制的原理 * 异常是什么:就是错误的另外一种说法; * 在java中,有一个专门模拟所有异常的类,所有的异常都必须继承这个类:Throwable; * 本质是:当程序出错以后,jvm ...

  9. apache 2.4 You don't have permission to access / on this server

    用的2.4版本,以前版本解决: 马上打开apache的配置文件httpd.conf,逐行检查.在大约快一半的地方有以下这段代码: <Directory />    Options Foll ...

  10. (转)jQuery LigerUI 插件介绍及使用之ligerTree

    一,简介  ligerTree的功能列表: 1,支持本地数据和服务器数据(配置data或者url) 2,支持原生html生成Tree 3,支持动态获取增加/修改/删除节点 4,支持大部分常见的事件 5 ...