给定一颗二叉搜索树,请找出其中的第k小的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。
// 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;
}
给定一颗二叉搜索树,请找出其中的第k小的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。的更多相关文章
- 给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x ...
- 小白专场-是否同一颗二叉搜索树-c语言实现
目录 一.题意理解 二.求解思路 三.搜索树表示 程序框架搭建 3.1 如何建搜索树 3.2 如何判别 3.3 清空树 更新.更全的<数据结构与算法>的更新网站,更有python.go.人 ...
- 小白专场-是否同一颗二叉搜索树-python语言实现
目录 一.二叉搜索树的相同判断 二.问题引入 三.举例分析 四.方法探讨 4.1 中序遍历 4.2 层序遍历 4.3 先序遍历 4.4 后序遍历 五.总结 六.代码实现 一.二叉搜索树的相同判断 二叉 ...
- HDU 3791 二叉搜索树 (数据结构与算法实验题 10.2 小明) BST
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3791 中文题不说题意. 建立完二叉搜索树后进行前序遍历或者后序遍历判断是否一样就可以了. 跟这次的作业第 ...
- leetcode-第10周双周赛-5080-查找两颗二叉搜索树之和
题目描述: 自己的提交: class Solution: def twoSumBSTs(self, root1: TreeNode, root2: TreeNode, target: int) -&g ...
- Convert Sorted Array to Binary Search Tree(将一个有序数组转换成一颗二叉搜索树)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- 二叉搜索树的第k个结点
给定一颗二叉搜索树,请找出其中的第k小的结点.例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4. /* public class TreeNode { ...
- 二叉搜索树的第 k 个结点
题目 给定一颗二叉搜索树,请找出其中的第k小的结点,即将二叉树中所有元素从小到大排序的第 k 个结点. 解析 按中序遍历二叉搜索树就可以获得一个非递减的序列,此时第 k 个就为答案.实际上我们只需要按 ...
- C++版 - 剑指offer 面试题63:二叉搜索树的第k个结点(二叉树中序遍历的应用) 题解
面试题 63:二叉搜索树的第k个结点 题目:给定一颗二叉搜索树,请找出其中的第k大的结点.例如, 5 / \ 3 7 /\ /\ 2 4 6 8 (见下面的图1) 中,按结点数值大小顺序第三个结点的值 ...
随机推荐
- webkit常见问题汇总
前段时间有人问我一个简单的问题,html如何创建解析的? 我讲了一大堆,什么通过DocumentLoader, CachedResourceLoader, CacheResource, Resourc ...
- 【SNMP】Linux系统下安装net-snmp
这里使用的snmp的版本是net-snmp-5.7.3下载地址:http://www.net-snmp.org/download.html 安装步骤: 1.解压缩安装包: tar -xzvf net- ...
- 【Linux C中文函数手册】文件内容控制函数
文件内容控制函数 1)clearerr 清除文件流的错误旗标 相关函数 feof表头文件 #include<stdio.h>定义函数 void clearerr(FILE * stream ...
- 集合类学习之Hashmap机制研究
1.遍历的两种实现方法 //新建 Map map=new HashMap(); //存储值 map.put() ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 //遍历方式 ...
- Hadoop集群错误
1.Hadoop集群所有的DataNode都启动不了解决办法 删除从节点.../usr/hadoop/tmp/dfs/ 下内容,再重新格式化namenode
- (转)集成架构:对比 Web API 与面向服务的架构和企业应用程序集成
摘要:总体上讲,SOA 和 Web API 似乎解决的是同一个问题:以实时的.可重用的方式公开业务功能.本教程将分析这些举措有何不同,以及如何将它们融入到一个不断演变的集成架构中.文中还将讨论 API ...
- 真正明白C语言二级指针(转载)
指针是C语言的灵魂,我想对于一级指针大家应该都很熟悉,也经常用到:比如说对于字符串的处理,函数参数的“值,结果传递”等,对于二级指针或者多级指针,我想理解起来也是比较容易的,比如二级指针就是指向指针的 ...
- bzoj 1042 HAOI2008 硬币购物
这道题思路是在是神. 先dp出没有限制时候的方案数. dp的时候注意 先循环 1..4 再循环 1..maxs 防止重复.边界是f[0] = 1. 这么基础的背包都忘记了=_= 接下来处理有重复的问题 ...
- Factory_Method
class Product { public: virtual ~Product() {} ; }; class ProductA : public Product { public: Product ...
- C# 带进度条的文件下载
private long fileLength; private long downLength;//已经下载文件大小,外面想用就改成公共属性 private static bool stopDown ...