Kth Smallest Element in a BST
2015-07-02 10:53
381 查看
https://leetcode.com/problems/kth-smallest-element-in-a-bst/
Given a binary search tree, write a function
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).
解题思路:
带返回值的递归
不带返回值的递归
或者直接将step赋值为k也可以,省去一个参数
参数使用数组的原因是Java无法pass by reference,或者使用全局变量,也是可以的。
Given a binary search tree, write a function
kthSmallestto 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).
解题思路:
带返回值的递归
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int kthSmallest(TreeNode root, int k) { int [] step = new int[1]; step[0] = 1; TreeNode res = dfs(root, k, step); if(res == null) { return -1; } return res.val; } public TreeNode dfs(TreeNode root, int k, int[] step) { if(root == null) { return null; } TreeNode res1 = dfs(root.left, k, step); if(res1 != null) { return res1; } if(step[0] == k) { return root; } step[0]++; TreeNode res2 = dfs(root.right, k, step); return res2; } }
不带返回值的递归
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int kthSmallest(TreeNode root, int k) { int [] step = new int[1]; step[0] = 1; int [] res = new int[1]; dfs(root, k, step, res); return res[0]; } public void dfs(TreeNode root, int k, int[] step, int[] res) { if(root == null) { return; } dfs(root.left, k, step, res); if(step[0] == k) { res[0] = root.val; step[0]++; return; } step[0]++; dfs(root.right, k, step, res); } }
或者直接将step赋值为k也可以,省去一个参数
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int kthSmallest(TreeNode root, int k) { int [] step = new int[1]; step[0] = k; int [] res = new int[1]; dfs(root, step, res); return res[0]; } public void dfs(TreeNode root, int[] step, int[] res) { if(root == null) { return; } dfs(root.left, step, res); if(step[0] == 1) { res[0] = root.val; step[0]--; return; } step[0]--; dfs(root.right, step, res); } }
参数使用数组的原因是Java无法pass by reference,或者使用全局变量,也是可以的。
相关文章推荐
- Android背景音乐功能的实现
- 全文检索引擎Solr系列—–全文检索基本原理
- Tk 8.5 and Python nf.py 中的应用
- java 中xml转换为json对象
- 文章标题
- mac 安装maven 和修改java环境变量
- Chrome扩展及应用开发 入门笔记(四)
- H3C 胖AP设置(VLAN模式)――高级配置
- 顺序容器的基本操作
- iOS手动内存管理知识总结及发散思维
- JQuery 遍历子元素+ each函数的跳出+提取字符串中的数字
- ios关于layer的一些常用属性
- nodejs生成vs2008工程问题解决方案
- Flexslider轮播图片,一刷新出现多张图片问题解决
- matlab截取字符串
- IOS开发快捷键整理
- Html文档服务Resin3.0.24 文件读写Bug
- Waiting For Debugger
- STL中的二分查找--lower_bound/upper_bound/binary_search
- 大型商贸系统(进货管理)技术解析(十)查询返厂单