Kth Smallest Element in a BST
来源:互联网 发布:淘宝店铺层级怎么计算 编辑:程序博客网 时间:2024/04/25 22:14
思路:
1 . 可以设置一个全局变量 , 中序遍历即可
2. 不是递归总序遍历,而是迭代式
public class Solution { public int kthSmallest(TreeNode root, int k) { LinkedList<TreeNode> stack = new LinkedList<> (); stack = getLeft(root); int idx = 0; while(!stack.isEmpty()) { TreeNode node = stack.removeLast(); idx++; if(node.right != null) stack.addAll(getLeft(node.right)); if(idx == k) return node.val; } return 0; } LinkedList<TreeNode> getLeft(TreeNode root) { LinkedList<TreeNode> list = new LinkedList<> (); while(root != null) { list.addLast(root); root = root.left; } return list; }}
0 0
- #leetcode#Kth Smallest Element in a BST
- 230.Kth Smallest Element in a BST
- leetcode--Kth Smallest Element in a BST
- Kth Smallest Element in a BST
- Kth Smallest Element in a BST
- Leetcode|Kth Smallest Element in a BST
- Kth Smallest Element in a BST
- [leetcode] Kth Smallest Element in a BST
- 【LeetCode】Kth Smallest Element in a BST
- Kth Smallest Element in a BST
- LeetCode Kth Smallest Element in a BST
- Kth Smallest Element in a BST
- 【leetcode】Kth Smallest Element in a BST
- Kth Smallest Element in a BST
- Kth Smallest Element in a BST
- LeetCode Kth Smallest Element in a BST
- Kth Smallest Element in a BST
- leetcode: Kth Smallest Element in a BST
- 五招防止QQ密码被盗
- 数据结构学习---队列的链式储存实现
- Repeater和Gridview的区别
- Object类、String类、StringBuffer类、StringBuilder类
- 无法打开包括源文件"extensions/cocos-ext.h"
- Kth Smallest Element in a BST
- 并查集详解
- django1.8 view(6): Built-in Views
- 【前端学习笔记】JQuery的基本思想、常用方法
- HDU 2077 汉诺塔IV
- php调用c++程序例子
- Bean的作用域及初始化
- [第2章]多线程:GCD的简单使用(2)——调度组
- MongoDB查询