LeetCode OJ 230. Kth Smallest Element in a BST
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:
- Try to utilize the property of a BST.
- What if you could modify the BST node's structure?
- The optimal runtime complexity is O(height of BST).
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private int cnt = 0;
public int kthSmallest(TreeNode root, int k) {
int result = 0;
if(root != null){
result = kthSmallest(root.left,k);
cnt++;
if(cnt == k){
return root.val;
}
result += kthSmallest(root.right,k);
}
return result;
}
}
LeetCode OJ 230. Kth Smallest Element in a BST的更多相关文章
- 【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 ...
- 【刷题-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 ...
- 【LeetCode】230. Kth Smallest Element in a BST
Difficulty: Medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...
- LeetCode OJ: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 ...
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- [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 ...
- 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 ...
- (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 ...
- [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 ...
随机推荐
- python 基础学习-总结1
1.Python 简介 易学易懂,语法简单 不需编译,即可运行 比其他语言更简洁 不需要管理内存 1.1 什么是Python? python 是由Guido.van.Rossum于1989年始创,其根 ...
- Paxos算法简单介绍
一种基于消息传递且具有高度容错特性的一致性算法.解决在存在宕机或者网络异常的集群中对某个数据的值达成一致性,并且保证无论在发生以上任何异常都不会破坏整个系统的一致性,具有容错性. Paxos算法实现的 ...
- NetPayClient for PHP使用说明
名 称 放置的路径 用 途 SecssUtil.class.php 根据项目工程的需要放置对应路径下 支持PHP5.4.8及以上版本 用于提供商户签名.验签.加密.解密.文件验签等方法调用 Mer.p ...
- NPM使用介绍
NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用 允许用户NPM服务器下载并安装别 ...
- 查看源码利器之sublime text 3 配置 Ctags 插件
最近在看源码的时候发现sublime text 3是很给力的一款软件,小巧精致,这里着重讲解一下Ctags协助编译和跟踪函数 一.安装Package Control (如果Preferences &g ...
- 第一百一十节,JavaScript匿名函数和闭包
JavaScript匿名函数和闭包 学习要点: 1.匿名函数 2.闭包 匿名函数就是没有名字的函数,闭包是可访问一个函数作用域里变量的函数.声明:本节内容需要有面向对象和少量设计模式基础,否则无法听懂 ...
- dplyr 数据操作 列操作(select / mutate)
在R中,我们通常需要对数据列进行各种各样的操作,比如选取某一列.重命名某一列等. dplyr中的select函数子在数据列的操作上也同样表现了它的简洁性,而且各种操作眼花缭乱. select(.dat ...
- csu oj Infected Computer 1427
#include <iostream> #include <algorithm> #include <stdio.h> #define max 20005 #def ...
- LeetCode OJ 289. Game of Life
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- 阿里云 镜像 源 debian
/etc/apt/sources.list deb http://mirrors.aliyun.com/debian wheezy main contrib non-freedeb-src http: ...