《剑指offer》第六十八题(树中两个结点的最低公共祖先)
// 面试题68:树中两个结点的最低公共祖先
// 题目:输入两个树结点,求它们的最低公共祖先。 #include <iostream>
#include "Tree.h"
#include <list> using namespace std; bool GetNodePath(const TreeNode* pRoot, const TreeNode* pNode, list<const TreeNode*>& path)//找到根节点到指定节点的路径
{
if (pRoot == pNode)
return true; path.push_back(pRoot); bool found = false; vector<TreeNode*>::const_iterator i = pRoot->m_vChildren.begin();//m_vChildren是个数组,详见Tree.h
while (!found && i < pRoot->m_vChildren.end())
{
found = GetNodePath(*i, pNode, path);
++i;
} if (!found)
path.pop_back(); return found;
} const TreeNode* GetLastCommonNode//找最后一个公共节点
(
const list<const TreeNode*>& path1,
const list<const TreeNode*>& path2
)
{
list<const TreeNode*>::const_iterator iterator1 = path1.begin();
list<const TreeNode*>::const_iterator iterator2 = path2.begin();//迭代器 const TreeNode* pLast = nullptr; while (iterator1 != path1.end() && iterator2 != path2.end())
{
if (*iterator1 == *iterator2)
pLast = *iterator1; iterator1++;
iterator2++;
} return pLast;
} const TreeNode* GetLastCommonParent(const TreeNode* pRoot, const TreeNode* pNode1, const TreeNode* pNode2)
{
if (pRoot == nullptr || pNode1 == nullptr || pNode2 == nullptr)//判断边界
return nullptr; list<const TreeNode*> path1;
GetNodePath(pRoot, pNode1, path1); list<const TreeNode*> path2;
GetNodePath(pRoot, pNode2, path2); return GetLastCommonNode(path1, path2);
} // ====================测试代码====================
void Test(const char* testName, const TreeNode* pRoot, const TreeNode* pNode1, const TreeNode* pNode2, TreeNode* pExpected)
{
if (testName != nullptr)
printf("%s begins: ", testName); const TreeNode* pResult = GetLastCommonParent(pRoot, pNode1, pNode2); if ((pExpected == nullptr && pResult == nullptr) ||
(pExpected != nullptr && pResult != nullptr && pResult->m_nValue == pExpected->m_nValue))
printf("Passed.\n");
else
printf("Failed.\n");
} // 形状普通的树
// 1
// / \
// 2 3
// / \
// 4 5
// / \ / | \
// 6 7 8 9 10
void Test1()
{
TreeNode* pNode1 = CreateTreeNode();
TreeNode* pNode2 = CreateTreeNode();
TreeNode* pNode3 = CreateTreeNode();
TreeNode* pNode4 = CreateTreeNode();
TreeNode* pNode5 = CreateTreeNode();
TreeNode* pNode6 = CreateTreeNode();
TreeNode* pNode7 = CreateTreeNode();
TreeNode* pNode8 = CreateTreeNode();
TreeNode* pNode9 = CreateTreeNode();
TreeNode* pNode10 = CreateTreeNode(); ConnectTreeNodes(pNode1, pNode2);
ConnectTreeNodes(pNode1, pNode3); ConnectTreeNodes(pNode2, pNode4);
ConnectTreeNodes(pNode2, pNode5); ConnectTreeNodes(pNode4, pNode6);
ConnectTreeNodes(pNode4, pNode7); ConnectTreeNodes(pNode5, pNode8);
ConnectTreeNodes(pNode5, pNode9);
ConnectTreeNodes(pNode5, pNode10); Test("Test1", pNode1, pNode6, pNode8, pNode2);
} // 树退化成一个链表
// 1
// /
// 2
// /
// 3
// /
// 4
// /
//
void Test2()
{
TreeNode* pNode1 = CreateTreeNode();
TreeNode* pNode2 = CreateTreeNode();
TreeNode* pNode3 = CreateTreeNode();
TreeNode* pNode4 = CreateTreeNode();
TreeNode* pNode5 = CreateTreeNode(); ConnectTreeNodes(pNode1, pNode2);
ConnectTreeNodes(pNode2, pNode3);
ConnectTreeNodes(pNode3, pNode4);
ConnectTreeNodes(pNode4, pNode5); Test("Test2", pNode1, pNode5, pNode4, pNode3);
} // 树退化成一个链表,一个结点不在树中
// 1
// /
// 2
// /
// 3
// /
// 4
// /
//
void Test3()
{
TreeNode* pNode1 = CreateTreeNode();
TreeNode* pNode2 = CreateTreeNode();
TreeNode* pNode3 = CreateTreeNode();
TreeNode* pNode4 = CreateTreeNode();
TreeNode* pNode5 = CreateTreeNode(); ConnectTreeNodes(pNode1, pNode2);
ConnectTreeNodes(pNode2, pNode3);
ConnectTreeNodes(pNode3, pNode4);
ConnectTreeNodes(pNode4, pNode5); TreeNode* pNode6 = CreateTreeNode(); Test("Test3", pNode1, pNode5, pNode6, nullptr);
} // 输入nullptr
void Test4()
{
Test("Test4", nullptr, nullptr, nullptr, nullptr);
} int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
system("pause");
return ;
}
《剑指offer》第六十八题(树中两个结点的最低公共祖先)的更多相关文章
- (剑指Offer)面试题50:树中两个结点的最低公共祖先
题目: 求树中两个结点的最低公共祖先 思路: 考虑一下几种情况: 1.该树为二叉搜索树 二叉搜索树是排序树,位于左子树点的结点都比父结点小,而位于右子树的结点都比父结点大,只需要从树的根结点开始和两个 ...
- 【剑指Offer面试编程题】题目1509:树中两个结点的最低公共祖先--九度OJ
题目描述: 给定一棵树,同时给出树中的两个结点,求它们的最低公共祖先. 输入: 输入可能包含多个测试样例. 对于每个测试案例,输入的第一行为一个数n(0<n<1000),代表测试样例的个数 ...
- 剑指Offer - 九度1509 - 树中两个结点的最低公共祖先
剑指Offer - 九度1509 - 树中两个结点的最低公共祖先2014-02-07 01:04 题目描述: 给定一棵树,同时给出树中的两个结点,求它们的最低公共祖先. 输入: 输入可能包含多个测试样 ...
- 【Java】 剑指offer(68) 树中两个结点的最低公共祖先
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 输入两个树结点,求它们的最低公共祖先. 思路 该题首先要和面试 ...
- 【Offer】[68] 【树中两个结点的最低公共祖先】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 输入两个树结点,求它们的最低公共祖先. [牛客网刷题地址]无 思路分析 该题首先要确定是否为二叉树,还要确定是否为二叉搜索树,是否有父指 ...
- 《剑指offer》第二十八题(对称的二叉树)
// 面试题28:对称的二叉树 // 题目:请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和 // 它的镜像一样,那么它是对称的. #include <iostream> ...
- 《剑指offer》第十八题(删除链表中重复的结点)
// 面试题18(二):删除链表中重复的结点 // 题目:在一个排序的链表中,如何删除重复的结点?例如,在图3.4(a)中重复 // 结点被删除之后,链表如图3.4(b)所示. #include &l ...
- 《剑指offer》第十八题(在O(1)时间删除链表结点)
// 面试题18(一):在O(1)时间删除链表结点 // 题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该 // 结点. #include <iostream> ...
- 【剑指Offer学习】【面试题50:树中两个结点的最低公共祖先】
题目:求树中两个结点的最低公共祖先,此树不是二叉树,而且没有指向父节点的指针. 树的结点定义 private static class TreeNode { int val; List<Tree ...
随机推荐
- End of script output before headers错误解决方法
注意是否丢失两个换行符\n\n printf("Content-type: text/html;charset=utf-8\n\n");
- jbpm 6 vs activities 5评估(持续更新、亲测实际项目评估)
最近我们有个使用了jbpm 6.2(6.2是一个较大的里程碑)的批处理模块,因为大BOSS一直觉得太重了,希望有更加轻量级的解决方案,因为我们基本上没有真正意义上流程的概念,只有静态的流程图,因为一直 ...
- Tree Implementation with Python
Tree Implementation with Python List of List 代码如下: def binary_tree(val): return [val, [], []] def in ...
- 02: docker高级篇
1.1 Docker Compose 1.Docker Compose 介绍 1. Compose是一个定义和管理多容器的工具,使用Python语言编写. 2. 使用Compose配置文件描述多个容器 ...
- java常用类-StringBuffer,Integer,Character
* StringBuffer: * 线程安全的可变字符串. * * StringBuffer和String的区别? * 前者长度和内容可变,后者不可变. * 如果使用前者做字符串的拼接,不会浪费太多的 ...
- Overture 5入门之如何设置延音线
延音线作为五线谱标记符合之一,是大家使用Overture时需要常用的一个基本标记符号.延音线是一条向上或向下弯曲的弧线,它的作用是将两个或两个以上的具有相同高音的音符来进行相连. 延音线作为再编曲演唱 ...
- HTTP 压力测试工具
http_load 程序非常小,解压后也不到100K http_load以并行复用的方式运行,用以测试web服务器的吞吐量与负载.但是它不同于大多数压力测试工 具,它可以以一个单一的进程运行,一般不会 ...
- 【做题】agc006C - Rabbit Exercise——模型转换
原文链接https://www.cnblogs.com/cly-none/p/9745177.html 题意:数轴上有\(n\)个点,从\(1\)到\(n\)编号.有\(m\)个操作,每次操作给出一个 ...
- (转)ElasticSearch学习
(二期)21.全文搜索引擎Elasticsearch [课程21]elasticsearch.xmind82.1KB [课程21]lucene.xmind0.8MB [课程21]基本用法....api ...
- Win32汇编学习(3):简单的窗口
这次我们将写一个 Windows 程序,它会在桌面显示一个标准的窗口,以此根据代码来学习如何创建一个简单的窗口. 理论: Windows 程序中,在写图形用户界面时需要调用大量的标准 Windows ...