《剑指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 ...
随机推荐
- TensorFlow学习---tf.nn.dropout防止过拟合
一. Dropout原理简述: tf.nn.dropout是TensorFlow里面为了防止或减轻过拟合而使用的函数,它一般用在全连接层. Dropout就是在不同的训练过程中随机扔掉一部分神经元.也 ...
- Git总结笔记
git相关配置 # 设置你的 Git 用户名 git config --global user.name "<Your-Full-Name>" # 设置你的 Git 邮 ...
- NOIP 2016 天天爱跑步 (luogu 1600 & uoj 261) - 线段树
题目传送门 传送点I 传送点II 题目大意 (此题目不需要大意,我认为它已经很简洁了) 显然线段树合并(我也不知道哪来这么多显然) 考虑将每条路径拆成两条路径 s -> lca 和 t -> ...
- Codeforces Round #427 (Div. 2) Problem A Key races (Codeforces 835 A)
Two boys decided to compete in text typing on the site "Key races". During the competition ...
- VS2012创建ATL工程及使用MFC测试COM组件
一.创建ATL工程 1.创建ATL项目,取名为ATLMyCom 2.在ATL项目向导中,勾选[支持MFC](利用MFC测试用).[支持 COM+ 1.0],其余的选项默认,点击完成. 3.右键工程名称 ...
- Received empty response from Zabbix Agent at[172.16.1.51]. Assuming that agent dropped connection because of access permissions
Centos7.5 Zabbix创建主机ZBX爆红 原因:/etc/zabbix/zabbix_agentd.conf配置文件的Server写错了 解决方法: [root@db01 ~]# vim / ...
- topcoder srm 475 div1
problem1 link 暴力枚举$r$只兔子的初始位置,然后模拟即可. problem2 link 假设刚生下来的兔子是1岁,那么能够生小兔子的兔子的年龄是至少3岁. 那么所有的兔子按照年龄可以分 ...
- Oracle SQL——inner jion;left join;right join的区别和使用场景
背景 在一次面试的时候,面试官让我说一下这三者的使用场景和区别,当时瞬间懵逼,哈哈.回来赶快看一看,记下来. 详解 inner join 等值查询:返回两张表中,联结字段值相等的组合记录 举例:所有学 ...
- cmd copy命令 文件复制【转】
本文转载自:https://www.jb51.net/article/18981.htm copy,中文含义为“复制”,一个很容易见名知意的命令,它的作用是复制文件,用法十分简单:copy 源文件 目 ...
- cygwin下如何编译安装minicom?
1. 安装依赖的软件和库 apt-cyg install autoconf automake make libncurses-devel (apt-cyg工具的安装方法在此) 2. 获取源码 wget ...