// 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。的更多相关文章

  1. 给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。

    /* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x ...

  2. 小白专场-是否同一颗二叉搜索树-c语言实现

    目录 一.题意理解 二.求解思路 三.搜索树表示 程序框架搭建 3.1 如何建搜索树 3.2 如何判别 3.3 清空树 更新.更全的<数据结构与算法>的更新网站,更有python.go.人 ...

  3. 小白专场-是否同一颗二叉搜索树-python语言实现

    目录 一.二叉搜索树的相同判断 二.问题引入 三.举例分析 四.方法探讨 4.1 中序遍历 4.2 层序遍历 4.3 先序遍历 4.4 后序遍历 五.总结 六.代码实现 一.二叉搜索树的相同判断 二叉 ...

  4. HDU 3791 二叉搜索树 (数据结构与算法实验题 10.2 小明) BST

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3791 中文题不说题意. 建立完二叉搜索树后进行前序遍历或者后序遍历判断是否一样就可以了. 跟这次的作业第 ...

  5. leetcode-第10周双周赛-5080-查找两颗二叉搜索树之和

    题目描述: 自己的提交: class Solution: def twoSumBSTs(self, root1: TreeNode, root2: TreeNode, target: int) -&g ...

  6. 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 ...

  7. 二叉搜索树的第k个结点

    给定一颗二叉搜索树,请找出其中的第k小的结点.例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4. /* public class TreeNode { ...

  8. 二叉搜索树的第 k 个结点

    题目 给定一颗二叉搜索树,请找出其中的第k小的结点,即将二叉树中所有元素从小到大排序的第 k 个结点. 解析 按中序遍历二叉搜索树就可以获得一个非递减的序列,此时第 k 个就为答案.实际上我们只需要按 ...

  9. C++版 - 剑指offer 面试题63:二叉搜索树的第k个结点(二叉树中序遍历的应用) 题解

    面试题 63:二叉搜索树的第k个结点 题目:给定一颗二叉搜索树,请找出其中的第k大的结点.例如, 5 / \ 3 7 /\ /\ 2 4 6 8 (见下面的图1) 中,按结点数值大小顺序第三个结点的值 ...

随机推荐

  1. c++错误修复 数据库无法打开 无法右击 run outtiime

    先前有安装vs2015失败的前提 现象:1.无法右击  显示 explorer.exe   c++        run outtiime  这些字样 2. 安装有数据库的软件都不能用.显示数据库连接 ...

  2. UI5_UIAlertView与UIActionSheet

    // // ViewController.h // UI5_UIAlertView与UIActionSheet // // Created by zhangxueming on 15/7/7. // ...

  3. Spring IoC容器的设计——BeanFactory应用场景2

    1.BeanFactory接口设计了getBean方法,这个方法是使用IoC容器API的主要方法,通过这个方法,可以取得IoC容器中管理的Bean,Bean的取得是通过指定名字来索引的. 2.如果需要 ...

  4. trident 序列号问题

    在使用Storm的trident做流计算开发时,遇到一个诡异的问题: 我继承IPartitionedTridentSpout或者IOpaquePartitionedTridentSpout接口做事务型 ...

  5. Geodatabase介绍

    一.概述 (1)Geodatabase是什么? ArcGIS操作基于GIS文件格式和存储于地理数据库(Geodatabase)中的地理信息.Geodatabase是ArcGIS的本地数据结构,是用于编 ...

  6. Codevs 2833 奇怪的梦境

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold     题目描述 Description Aiden陷入了一个奇怪的梦境:他被困在一个小房子中,墙上有很多按钮,还 ...

  7. C++封装常用对象和对头文件以及预编译机制的探索

    在C++实际开发中,难免会使用到一些你极为常用的算法(比如笔者经常使用的多线程技术),实现这些算法的类或是全局函数或是命名空间等等经常都要被使用多次,你会有哪些办法来使用呢?笔者有4个办法. 第一个方 ...

  8. python——异常处理

    python处理异常的格式一般如下: try: A部分 Except  .... [as ..]: B部分 [ finally: C部分 Else: D部分 ] ps: 1.不管有没有异常,final ...

  9. Lucene 4.0

    关于4.0的Update Index  ,Create Index /* * Create Index */ public static void createIndex() throws IOExc ...

  10. Winform 拦截最小化、最大化、关闭事件【整理】

    const int WM_SYSCOMMAND = 0x112; //窗体关闭消息 const int SC_CLOSE = 0xf060; //窗体最小化消息 const int SC_MINIMI ...