1 Star 0 Fork 0

表情扭曲 / leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc230.java 870 Bytes
一键复制 编辑 原始数据 按行查看 历史
liu13 提交于 2019-02-27 10:26 . 20190227
package code;
import java.util.Stack;
/*
* 230. Kth Smallest Element in a BST
* 题意:二叉搜索树种第k小的数
* 难度:Medium
* 分类:Binary Search, Tree
* 思路:每次找到最小的值,k--。中序遍历。
* Tips:非递归中序遍历还是不熟练。。。
*/
public class lc230 {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public int kthSmallest(TreeNode root, int k) {
Stack<TreeNode> st = new Stack();
while(!st.isEmpty()||root!=null){
while(root!=null) {
st.add(root);
root = root.left;
}
root = st.pop();
k--;
if(k==0) return root.val;
root = root.right;
}
return 0;
}
}
1
https://gitee.com/abfantasy/leetcode.git
git@gitee.com:abfantasy/leetcode.git
abfantasy
leetcode
leetcode
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891