《剑指offer》第五十四题(二叉搜索树的第k个结点)
// 面试题54:二叉搜索树的第k个结点
// 题目:给定一棵二叉搜索树,请找出其中的第k大的结点。 #include <iostream>
#include "BinaryTree.h" const BinaryTreeNode* KthNodeCore(const BinaryTreeNode* pRoot, unsigned int& k); const BinaryTreeNode* KthNode(const BinaryTreeNode* pRoot, unsigned int k)
{
if (pRoot == nullptr || k == )//判断边界
return nullptr; return KthNodeCore(pRoot, k);
} const BinaryTreeNode* KthNodeCore(const BinaryTreeNode* pRoot, unsigned int& k)//k必须引用传递啊,不然记不住
{
const BinaryTreeNode* target = nullptr; if (pRoot->m_pLeft != nullptr)//迭代找出最小点
target = KthNodeCore(pRoot->m_pLeft, k); if (target == nullptr)//找到最小节点了,并k--,如果k==1,就算是找到了,记下target
{
if (k == )
target = pRoot; k--;
} if (target == nullptr && pRoot->m_pRight != nullptr)//一旦找到target,就不会继续迭代了,全部返回
target = KthNodeCore(pRoot->m_pRight, k); return target;
} // ====================测试代码====================
void Test(const char* testName, const BinaryTreeNode* pRoot, unsigned int k, bool isNull, int expected)
{
if (testName != nullptr)
printf("%s begins: ", testName); const BinaryTreeNode* pTarget = KthNode(pRoot, k);
if ((isNull && pTarget == nullptr) || (!isNull && pTarget->m_nValue == expected))
printf("Passed.\n");
else
printf("FAILED.\n");
} // 8
// 6 10
// 5 7 9 11
void TestA()
{
BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
BinaryTreeNode* pNode10 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode7 = CreateBinaryTreeNode();
BinaryTreeNode* pNode9 = CreateBinaryTreeNode();
BinaryTreeNode* pNode11 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode8, pNode6, pNode10);
ConnectTreeNodes(pNode6, pNode5, pNode7);
ConnectTreeNodes(pNode10, pNode9, pNode11); Test("TestA0", pNode8, , true, -);
Test("TestA1", pNode8, , false, );
Test("TestA2", pNode8, , false, );
Test("TestA3", pNode8, , false, );
Test("TestA4", pNode8, , false, );
Test("TestA5", pNode8, , false, );
Test("TestA6", pNode8, , false, );
Test("TestA7", pNode8, , false, );
Test("TestA8", pNode8, , true, -); DestroyTree(pNode8); printf("\n\n");
} // 5
// /
// 4
// /
// 3
// /
// 2
// /
//
void TestB()
{
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode3 = CreateBinaryTreeNode();
BinaryTreeNode* pNode2 = CreateBinaryTreeNode();
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode5, pNode4, nullptr);
ConnectTreeNodes(pNode4, pNode3, nullptr);
ConnectTreeNodes(pNode3, pNode2, nullptr);
ConnectTreeNodes(pNode2, pNode1, nullptr); Test("TestB0", pNode5, , true, -);
Test("TestB1", pNode5, , false, );
Test("TestB2", pNode5, , false, );
Test("TestB3", pNode5, , false, );
Test("TestB4", pNode5, , false, );
Test("TestB5", pNode5, , false, );
Test("TestB6", pNode5, , true, -); DestroyTree(pNode5); printf("\n\n");
} // 1
// \
// 2
// \
// 3
// \
// 4
// \
//
void TestC()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
BinaryTreeNode* pNode2 = CreateBinaryTreeNode();
BinaryTreeNode* pNode3 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode1, nullptr, pNode2);
ConnectTreeNodes(pNode2, nullptr, pNode3);
ConnectTreeNodes(pNode3, nullptr, pNode4);
ConnectTreeNodes(pNode4, nullptr, pNode5); Test("TestC0", pNode1, , true, -);
Test("TestC1", pNode1, , false, );
Test("TestC2", pNode1, , false, );
Test("TestC3", pNode1, , false, );
Test("TestC4", pNode1, , false, );
Test("TestC5", pNode1, , false, );
Test("TestC6", pNode1, , true, -); DestroyTree(pNode1); printf("\n\n");
} // There is only one node in a tree
void TestD()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(); Test("TestD0", pNode1, , true, -);
Test("TestD1", pNode1, , false, );
Test("TestD2", pNode1, , true, -); DestroyTree(pNode1); printf("\n\n");
} // empty tree
void TestE()
{
Test("TestE0", nullptr, , true, -);
Test("TestE1", nullptr, , true, -); printf("\n\n");
} int main(int argc, char* argv[])
{
TestA();
TestB();
TestC();
TestD();
TestE(); system("pause");
}
《剑指offer》第五十四题(二叉搜索树的第k个结点)的更多相关文章
- 面试题五十四:二叉搜索树的第K大节点
方法:搜索二叉树的特点就是左树小于节点,节点小于右树,所以采用中序遍历法就可以得到排序序列 BinaryTreeNode KthNode(BinaryTreeNode pNode ,int k){ i ...
- 《剑指offer》第十四题(剪绳子)
// 面试题:剪绳子 // 题目:给你一根长度为n绳子,请把绳子剪成m段(m.n都是整数,n>1并且m≥1). // 每段的绳子的长度记为k[0].k[1].…….k[m].k[0]*k[1]* ...
- 《剑指offer》第二十四题(反转链表)
// 面试题24:反转链表 // 题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的 // 头结点. #include <iostream> #include &quo ...
- 《剑指offer》第十八题(在O(1)时间删除链表结点)
// 面试题18(一):在O(1)时间删除链表结点 // 题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该 // 结点. #include <iostream> ...
- (剑指Offer)面试题27:二叉搜索树与双向链表
题目: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 二叉树的定义如下: struct TreeNode{ int val; Tr ...
- (剑指Offer)面试题24:二叉搜索树的后序遍历序列
题目: 输入一个整数数组,判断该数组是不是某个二叉搜索树的后序遍历的结果,如果是则返回true,否则返回false. 假设输入的数组的任意两个数字都互不相同. 思路: 根据二叉搜索树的后序遍历特点,很 ...
- 【剑指offer】面试题24:二叉搜索树的后序遍历序列
题目: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 思路: 递归 注意,主要就是假定数组为空时结果为fa ...
- 【剑指offer】面试题27:二叉搜索树与双向链表
题目: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 思路: 假设已经处理了一部分(转换了左子树),则得到一个有序的双向链表,现在 ...
- 【剑指offer】面试题24:二叉搜索树的兴许前序遍历序列
分析: 前序: 根 左 右 后序: 左 由 根 二叉搜索树: 左 < 根 < 右 那么这就非常明显了. def ifpost(postArray, start, end): #one or ...
- 《剑指offer》— JavaScript(26)二叉搜索树与双向链表
二叉搜索树与双向链表 题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 思路 递归思想:把大问题转换为若干小问题: 由于Ja ...
随机推荐
- thinkphp 检测验证码
/** * 检测验证码 * @param integer $id 验证码ID * @return boolean 检测结果 */function check_verify($code, $id = 1 ...
- django 表单常用field
BooleanField字段:相当于单选框 CharField:接受字符串 参数:max_length最大长度,min_length最小长度 require字段是否是必须的,默认为required=T ...
- java bean属性拷贝工具类比对(性能+功能)
业务系统中经常需要两个对象进行属性的拷贝,不能否认逐个的对象拷贝是最快速最安全的做法,但是当数据对象的属性字段数量超过程序员的容忍的程度比如通常超过5个属性的时候,代码因此变得臃肿不堪,使用一些方便的 ...
- 【题解】Luogu P1204 [USACO1.2]挤牛奶Milking Cows
原题传送门:P1204 [USACO1.2]挤牛奶Milking Cows 实际是道很弱智的题目qaq 但窝还是觉得用珂朵莉树写会++rp(窝都初二了,还要考pj) 前置芝士:珂朵莉树 窝博客里对珂朵 ...
- 【题解】Luogu P2319 [HNOI2006]超级英雄
原题传送门 这道题就是一个很简单的二分图匹配 二分图匹配详解 一开始想的是2-sat和网络流,根本没想匈牙利和HK 这道题只要注意一点:当一个点匹配不成功之后就直接退出 剩下的就写个二分图最大匹配就行 ...
- springboot 事务回滚
在springboot中,使用事务回滚时,添加@Transactional注解,然后在try-catch块中,发生异常时,在catch中 添加 TransactionAspectSupport.cur ...
- android 导出apk
一个困扰了几个月的问题在今天得以解决,运动益智可能有点过,能让一个人思路清晰倒是真! 问题描述:本地调试运行及不加密导出apk运行正常,当加密生成apk安装后,从接口返回的数据总是空.尝试过各种配置, ...
- ubuntu+anaconda+tensorflow 及相关问题
配置tensorflow部分参考:https://blog.csdn.net/XUTIAN1129/article/details/78997633 装完anaconda, source ~/.bas ...
- Java TreeSet的定制排序
注:只贴出实现类 package Test3; import java.util.Comparator;import java.util.TreeSet; public class Test { pu ...
- Python常用库之Pilow
基本用法 静态方法 PIL.Image.open(fp, mode=’r’) 传入文件路径(str),返回一个image对象 PIL.Image.alpha_composite(im1, im2) 混 ...