leetcode--Kth Smallest Element in a BST
2015-08-01 15:03
211 查看
Given a binary search tree, write a function
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).
题意:在BST树中,查找第k小的节点值
分类:二叉树
解法1:BST树很多问题我们都可以使用中序遍历来解决,这个也不例外
我们设置一个当前访问的元素个数cur,在中序遍历过程中,每要访问一个节点,cur加1
当cur和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 cur = 0;
public int kthSmallest(TreeNode root, int k) {
TreeNode res = inorder(root,k);
if(res!=null) return res.val;
return -1;
}
public TreeNode inorder(TreeNode root,int k){
if(root==null) return null;
if(root.left!=null){
TreeNode left = inorder(root.left,k);//先在左子树找
if(left!=null) return left;//如果找到了,返回
}
cur++;//每次访问节点,都加1
if(cur==k) return root;//判断是不是当前节点
if(root.right!=null){//最后在右子树找
TreeNode right = inorder(root.right,k);
if(right!=null) return right;
}
return null;
}
}
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).
题意:在BST树中,查找第k小的节点值
分类:二叉树
解法1:BST树很多问题我们都可以使用中序遍历来解决,这个也不例外
我们设置一个当前访问的元素个数cur,在中序遍历过程中,每要访问一个节点,cur加1
当cur和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 cur = 0;
public int kthSmallest(TreeNode root, int k) {
TreeNode res = inorder(root,k);
if(res!=null) return res.val;
return -1;
}
public TreeNode inorder(TreeNode root,int k){
if(root==null) return null;
if(root.left!=null){
TreeNode left = inorder(root.left,k);//先在左子树找
if(left!=null) return left;//如果找到了,返回
}
cur++;//每次访问节点,都加1
if(cur==k) return root;//判断是不是当前节点
if(root.right!=null){//最后在右子树找
TreeNode right = inorder(root.right,k);
if(right!=null) return right;
}
return null;
}
}
相关文章推荐
- php通过pdo连接mssql
- linux的socket_tcp服务端与 linux的socket_tcp客服端简单通讯
- 那些Xcode不能错过的插件
- 卫计委电子病历架构基本概念
- NYOJ269VF【dp】
- Jedis - 基本建模图
- gsoap创建webservice服务简单教程
- hdu 2028
- oj上常见错误提示
- STM32 HAL库头文件包含关系
- POJ 2528 Mayor's posters
- Xcode 插件失效的临时解决方案
- Windows 10正式版字体乱码显示为方块怎么办?
- hdoj.5112 A Curious Matt【sort+暴力】 2015/08/01
- C#(WinForm) ComboBox和ListBox添加项
- 暑期训练--阶段总结
- [python]如何快速地翻转字符串
- clk prepare和enable的问题
- ioctl()在LINUX设备驱动中的用法(转)
- Codeforces 556A Case of the Zeros and Ones(消除01)