leetCode(46):Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth
smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
直观地一想。查找第k小的数,不就是遍历到第k个数吗?所以中序遍历非常easy想到,例如以下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
stack<TreeNode*> nodeStack;
TreeNode* tmp=root;
int kthMin = 0;
int kthValue;
while ((!nodeStack.empty() || tmp!=NULL) && kthMin!=k)
{
while (tmp != NULL)
{
nodeStack.push(tmp);
tmp = tmp->left;
}
tmp = nodeStack.top();
nodeStack.pop();
kthMin++;//对中序遍历稍加改动
kthValue = tmp->val;
tmp = tmp->right;
}
return kthValue;
}
};
另外。看了一下网友的解答,很巧妙。
他是先统计左子树上节点个数,假设节点个数小于k。则在右子树上找第k-n-1小的数,假设刚为k则就是当前节点,假设大于k,则继续在左子树上找第k小的数。
只是。每次递归都要统计一次节点个数,会不会导致复杂度添加?
int kthSmallest(TreeNode* root, int k) {
if (!root) return 0;
if (k==0) return root->val;
int n=count_size(root->left);
if (k==n+1) return root->val;
if (n>=k){
return kthSmallest(root->left, k);
}
if (n<k){
return kthSmallest(root->right, k-n-1);
}
}
int count_size(TreeNode* root){
if (!root) return 0;
return 1+count_size(root->left)+count_size(root->right);
}
leetCode(46):Kth Smallest Element in a BST的更多相关文章
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- Leetcode 230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- [LeetCode] 230. Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- (medium)LeetCode 230.Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- [LeetCode] 230. Kth Smallest Element in a BST 解题思路
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- Java for LeetCode 230 Kth Smallest Element in a BST
解题思路: 直接修改中序遍历函数即可,JAVA实现如下: int res = 0; int k = 0; public int kthSmallest(TreeNode root, int k) { ...
- LeetCode 230 Kth Smallest Element in a BST 二叉搜索树中的第K个元素
1.非递归解法 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...
- LeetCode 230. Kth Smallest Element in a BST 动态演示
返回排序二叉树第K小的数 还是用先序遍历,记录index和K进行比较 class Solution { public: void helper(TreeNode* node, int& idx ...
- LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)
230. 二叉搜索树中第K小的元素 230. Kth Smallest Element in a BST 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的 ...
随机推荐
- HDU TIANKENG’s rice shop(模拟)
HDU 4884 TIANKENG's rice shop 题目链接 题意:模拟题.转一篇题意 思路:就模拟就可以.注意每次炒完之后就能够接单 代码: #include <cstdio> ...
- CodeForces--606A --Magic Spheres(模拟水题)
Magic Spheres Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submi ...
- hdoj--2120--Ice_cream's world I(并查集判断环)
Ice_cream's world I Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 【BZOJ 2152】 聪聪可可
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2152 [算法] 点分治 [代码] #include<bits/stdc++.h ...
- 微软认证Hyper-V咨询工程师认证课程
课程链接:http://www.microsoftvirtualacademy.com/colleges/hyper-V-Certificate STEP 1:完成课程链接内的认证课程. STEP 2 ...
- android studio开发去掉titlebar
android:theme="@style/AppTheme"换成android:theme="@style/Theme.AppCompat.NoActionBar&qu ...
- java8 stream 流 例子
Trader raoul = new Trader("Raoul", "Cambridge"); Trader mario = new Trader(" ...
- jquery根据滚动条动态加载数据
PHP Code <div id="container"> <?php $query=mysql_query("select * from conten ...
- Unity脚本中可以引用的类型
Hierarchy(层级视图)面板里的对象,或者 Project(工程视图)里的Prefab.
- ZBrush中的实时遮罩
在ZBrush®中有许多遮罩类型,包括柔滑遮罩.反转遮罩,实时遮罩等.其中,实时遮罩又包含很多种类,它不同于一般的遮罩是不显示的,实时遮罩是根据实时信息产生新的遮罩. 在“Brush”菜单下“Auto ...