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小的数,不就是...
分类:
其他好文 时间:
2015-07-22 16:22:04
阅读次数:
87
为了彻底理解树状数组,试着用树状数组做了下普通平衡树而树状数组只能离线做,或者保证值的大小在数组可承受的范围内也是可以的,因为要求离线是因为必须事前对所有数离散化。然后我们看刘汝佳蓝书上的图利用如下代码,可以找到所有前缀和中第一个大于等于k的1 int kth(int k) {2 int a...
分类:
其他好文 时间:
2015-07-21 23:54:23
阅读次数:
216
Problem Definition:Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1].Note:Could you optimize yo...
分类:
其他好文 时间:
2015-07-21 22:11:28
阅读次数:
121
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.Follow up:What if the BST is modifi...
分类:
其他好文 时间:
2015-07-19 16:32:18
阅读次数:
137
2.2 Implement an algorithm to find the kth to last element of a singly linked list.这道题让我们求链表中倒数第k个元素,LeetCode中相类似的题目有Kth Largest Element in an Array 数...
分类:
其他好文 时间:
2015-07-19 13:21:05
阅读次数:
104
Given a binary search tree, write a functionkthSmallestto find thekth smallest element in it.Note:You may assume k is always valid, 1 ≤ k ≤ BST's tota...
分类:
其他好文 时间:
2015-07-15 18:33:34
阅读次数:
80
前几天在做Kth Largest Element in an Array 时使用到了堆,通过那倒题目也了解到了堆的make_heap,push_heap,pop_heap操作,看了C++ reference中的讲解也明白了heap_sort是什么回事。于是想着自己实现以下这四个函数。
堆的定义:
任意节点小于它的所有后裔,最小元在堆的根上(堆序性)。
堆总是一棵完全树。
#include <ios...
分类:
其他好文 时间:
2015-07-14 18:16:02
阅读次数:
109
题目:Given a binary search tree, write a functionkthSmallestto find thekth smallest element in it.Note:You may assume k is always valid, 1 ≤ k ≤ BST's t...
分类:
其他好文 时间:
2015-07-14 15:13:55
阅读次数:
98
class Solution {public: int kthSmallest(TreeNode* root, int k) { int num = 0; TreeNode*pNode=NULL; visit(root, k, num, pNode);...
分类:
其他好文 时间:
2015-07-14 15:01:24
阅读次数:
82
堆真是一种简单而又神奇的数据结构,以前用它求过前kth的数,现在又可以用两个堆来动态求解中位数。算法: 构建一个大顶堆和一个小顶堆,分别记为g和l。 假设当前中位数为mid,新读入一个数为tmp,则: 1.如果tmp = mid,则将tmp插入小顶堆,跳到步骤4。 3.如果大顶堆的元素个数比...
分类:
其他好文 时间:
2015-07-14 15:00:42
阅读次数:
93