给定一颗二叉搜索树,请找出其中的第k小的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。
2016-10-26 14:08
375 查看
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//
//
#include "stdafx.h" #include "stdafx.h" #include<iostream> #include<vector> #include<algorithm> #include<numeric> #include<list> #include<iterator> #include<queue> #include<stack> #include<algorithm> using namespace std; struct TreeNode { char val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; class Solution { public: TreeNode* KthNode(TreeNode* pRoot, unsigned int k) { getData(pRoot); if (k <= 0) return NULL; if (k > qu.size()) return NULL; for (int i = 1; i < k; i++) { qu.pop(); } return qu.front(); } queue<TreeNode*> qu; void getData(TreeNode* T) { if (T == NULL) return; else { getData(T->left); qu.push(T); getData(T->right); } } //序列化二叉树,前序创建二叉树 int p = -1; TreeNode* Deserialize(char *str) { if (*str == NULL) return NULL; //str为空 TreeNode *T = NULL; ++p; if (p >= strlen(str)) return NULL; //超出str的范围 if (str[p] == '#') return NULL; //str的值等于# T = new TreeNode(str[p]); T->left = Deserialize(str); T->right = Deserialize(str); return T; } void preOrder(TreeNode *T) { if (T == NULL) return; else { cout << T->val << " "; preOrder(T->left); preOrder(T->right); } } }; int main() { //int num = 5; //char *ch = new char[num]; //cout << "strlen(ch):" << strlen(ch)<<endl; //cout << endl; Solution so; TreeNode *T; int num = 5; char *str = "532##4##76##8##"; T = so.Deserialize(str); cout << "创建二叉树成功!"<<endl; cout << "前序遍历二叉树:" << endl; so.preOrder(T); cout << endl; TreeNode *re = so.KthNode(T,8); cout << "result:" << re->val << endl; //so.getData(T); //cout << "qu 队列中的值:" << endl; //so.print(); //cout << endl; return 0; }
相关文章推荐
- 《剑指offer》给定一颗二叉搜索树,请找出其中的第k大的结点。
- 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1},
- 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
- 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,
- 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
- 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1},
- 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回
- 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回
- 100亿个无序数,给定里面其中一个数A,找出与A大小相差最小的各10个数
- 给定n个矩阵{A1, A2, …,An},其中,Ai与Ai+1是可乘的,计算这n个矩阵的连乘积。从中找出一种乘次数最少的计算次序(矩阵连乘最优顺序Java语言实现
- 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数。 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在。
- A的大小为n,其中的每相邻的两个元素之间差的绝对值为1,例如A={4 5 6 5 6 7 8 9 10 9},给定A和t,设计一个程序,求出t在A中的位置
- 给定一组数,要求从中找出第k小的元素
- 给定一颗二叉树,以及其中的两个node(地址均非空),要求给出这两个node的一个公共父节点,使得这个父节点与两个节点的路径之和最小。
- 找出数组中第k大小的数,输出数所在的位置
- 给定一个无序整形数组,找出其中所以三个数之和为0的所有组合
- 新解:给定包含4 300 000 000个32位整数的顺序文件,如何找出一个至少出现两次的整数。
- 用JAVA写一个函数,功能例如以下: 随意给定一组数, 找出随意数相加之后的结果为35(随意设定)的情况
- 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个。 输入格式 第一行包含一个数n,表示序列长度。 第二行包含n个正整数,表示给定的序列。 第三个包含一个正整数m,表
- 程序员面试题目总结--数组(二)【二分查找、找出给定数字出现次数、两个有序整型数组交集、找出数组中唯一的重复元素、判断数组中的数值是否连续相邻】