(medium)LeetCode 230.Kth Smallest Element in a BST
2015-08-05 18:33
465 查看
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?
代码1:
运行结果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/03/15aa313751d877a95d47ca3d516a33db.png)
代码2:中序遍历递归
运行结果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/03/c8f180ff1e70aaf1c9ae3f77e4f2bbc4.png)
代码3:中序遍历迭代
运行结果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/03/2e59b16692eaca4534bcff7b0087030d.png)
代码4:使用队列,中序遍历,存储起来,然后出队n个元素即可。
运行结果:
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?
代码1:
/** * 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 count =countNodes(root.left); if(k<=count){ return kthSmallest(root.left,k); }else if(k>count+1){ return kthSmallest(root.right,k-1-count); } return root.val; } public int countNodes(TreeNode n){ if(n==null) return 0; return 1+countNodes(n.left)+countNodes(n.right); } }
运行结果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/03/15aa313751d877a95d47ca3d516a33db.png)
代码2:中序遍历递归
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { private static int number=0; private static int count=0; public int kthSmallest(TreeNode root, int k) { count=k; helper(root); return number; } public void helper(TreeNode n){ if(n.left!=null) helper(n.left); count--; if(count==0){ number=n.val; return; } if(n.right!=null) helper(n.right); } }
运行结果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/03/c8f180ff1e70aaf1c9ae3f77e4f2bbc4.png)
代码3:中序遍历迭代
/** * 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) { Stack<TreeNode>st=new Stack<>(); while(root!=null){ st.push(root); root=root.left; } while(k!=0){ TreeNode n=st.pop(); k--; if(k==0) return n.val; TreeNode right=n.right; while(right!=null){ st.push(right); right=right.left; } } return -1; } }
运行结果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/03/2e59b16692eaca4534bcff7b0087030d.png)
代码4:使用队列,中序遍历,存储起来,然后出队n个元素即可。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { private Queue<TreeNode> queue=new LinkedList<TreeNode>(); public int kthSmallest(TreeNode root, int k) { inOrder(root); TreeNode ret=null; while(k>0){ ret=queue.poll(); k--; } return ret.val; } public void inOrder(TreeNode root){ if(root==null) return; if(root.left!=null) inOrder(root.left); queue.offer(root); if(root.right!=null) inOrder(root.right); } }
运行结果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/03/2b7bec881fd1a227c5cca15984795006.png)
相关文章推荐
- Android中Activity四种启动模式和taskAffinity属性详解
- libcurl获取下载进度百分比,下载速度,剩余时间
- 如何通过浏览器分析前后端交互
- redis实战:使用redis实现自动补全
- 《新疆气象论文模版》
- POJ1505 Copying Books(二分法)
- dom4j
- Android-Intent与Bundle在传值上的区别
- 京东创始人刘强东创业史:女朋友父母认为我是耻辱
- Hadoop集群(第5期副刊)_JDK和SSH无密码配置
- 巧克力
- 叉积
- #设计模式
- dp入门基础题 FOJ1320 Ones
- Hadoop集群(第5期)_Hadoop安装配置
- 点击图片放大大图动画
- docker下基于flannel的overlay网络分析
- 超强、超详细Redis数据库入门教程
- ZOJ 2100 Seeding DFS
- 如何处理网站上多余、过时、杂项的内容