该题的思路很简单,就是对BST进行先序遍历,找到第k个数的时候返回。这里借助栈用迭代实现,递归的代码更简单,没有尝试。
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
stack cache;
TreeNode *point = root;
TreeNode...
分类:
其他好文 时间:
2015-07-12 17:27:38
阅读次数:
85
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,
Given [3,2,1,5,6,4] and k = 2, return 5.Note:...
分类:
其他好文 时间:
2015-07-09 22:41:43
阅读次数:
113
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 to...
分类:
其他好文 时间:
2015-07-09 14:40:05
阅读次数:
122
Kth Smallest Element in a BSTGiven a binary search tree, write a functionkthSmallestto find thekth smallest element in it.Note:You may assume k is alw...
分类:
其他好文 时间:
2015-07-08 12:24:28
阅读次数:
166
Kth Largest Element in an ArrayFind thekth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the ...
分类:
其他好文 时间:
2015-07-07 18:42:27
阅读次数:
98
1. 问题描述 给定一个二叉搜索树,找出第k小的元素。注意:可以假设k总是存在,1≤k≤BST总元素数1 \le k \le BST总元素数。2. 方法与思路 根据二叉搜索树的特点,中序遍历的结果即是排序好的数组。那么找出第k小的数,只需要先进行一次中序遍历即可。
/**
* Definition for a binary tree node.
* struct TreeNode {...
分类:
其他好文 时间:
2015-07-07 13:09:15
阅读次数:
119
leetcode 230: Kth Smallest Element in a BST
python java c++...
分类:
其他好文 时间:
2015-07-07 07:05:20
阅读次数:
125
基本思想就是:二叉树的中序非递归遍历 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *...
分类:
其他好文 时间:
2015-07-07 00:56:35
阅读次数:
114
这道题就是数点 divide and conquerclass Solution: # @param {TreeNode} root # @param {integer} k # @return {integer} def kthSmallest(self, root, k)...
分类:
其他好文 时间:
2015-07-07 00:55:14
阅读次数:
108
问题描述Ugly number is a number that only have factors 3, 5 and 7.Design an algorithm to find the Kth ugly number. The first 5 ugly numbers are 3, 5, 7, 9, 15 …问题分析这个题,有多种方法求解,常见的是有辅助数据结构,比如priority_queue,...
分类:
其他好文 时间:
2015-07-06 01:32:42
阅读次数:
533