该题的思路很简单,就是对BST进行先序遍历,找到第k个数的时候返回。这里借助栈用迭代实现,递归的代码更简单,没有尝试。
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
stack cache;
TreeNode *point = root;
TreeNode...
分类:
其他好文 时间:
2015-07-12 17:27:38
阅读次数:
85
def make_change(amount, coins): """Return a list of coins that sum to amount, preferring the smallest coins available and placing the smallest c...
分类:
其他好文 时间:
2015-07-12 00:03:57
阅读次数:
165
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
题目:It can be seen that the number, 125874, and its double, 251748, contains exactly the same digits, but in a different order. Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, ...
分类:
编程语言 时间:
2015-07-09 13:20:06
阅读次数:
158
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
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
Calling next() will return the next smallest number in the BST.
Note: next()...
分类:
其他好文 时间:
2015-07-07 22:50:37
阅读次数:
208
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