// 面试题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》第六十八题(树中两个结点的最低公共祖先)的更多相关文章

  1. (剑指Offer)面试题50:树中两个结点的最低公共祖先

    题目: 求树中两个结点的最低公共祖先 思路: 考虑一下几种情况: 1.该树为二叉搜索树 二叉搜索树是排序树,位于左子树点的结点都比父结点小,而位于右子树的结点都比父结点大,只需要从树的根结点开始和两个 ...

  2. 【剑指Offer面试编程题】题目1509:树中两个结点的最低公共祖先--九度OJ

    题目描述: 给定一棵树,同时给出树中的两个结点,求它们的最低公共祖先. 输入: 输入可能包含多个测试样例. 对于每个测试案例,输入的第一行为一个数n(0<n<1000),代表测试样例的个数 ...

  3. 剑指Offer - 九度1509 - 树中两个结点的最低公共祖先

    剑指Offer - 九度1509 - 树中两个结点的最低公共祖先2014-02-07 01:04 题目描述: 给定一棵树,同时给出树中的两个结点,求它们的最低公共祖先. 输入: 输入可能包含多个测试样 ...

  4. 【Java】 剑指offer(68) 树中两个结点的最低公共祖先

      本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 输入两个树结点,求它们的最低公共祖先. 思路 该题首先要和面试 ...

  5. 【Offer】[68] 【树中两个结点的最低公共祖先】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 输入两个树结点,求它们的最低公共祖先. [牛客网刷题地址]无 思路分析 该题首先要确定是否为二叉树,还要确定是否为二叉搜索树,是否有父指 ...

  6. 《剑指offer》第二十八题(对称的二叉树)

    // 面试题28:对称的二叉树 // 题目:请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和 // 它的镜像一样,那么它是对称的. #include <iostream> ...

  7. 《剑指offer》第十八题(删除链表中重复的结点)

    // 面试题18(二):删除链表中重复的结点 // 题目:在一个排序的链表中,如何删除重复的结点?例如,在图3.4(a)中重复 // 结点被删除之后,链表如图3.4(b)所示. #include &l ...

  8. 《剑指offer》第十八题(在O(1)时间删除链表结点)

    // 面试题18(一):在O(1)时间删除链表结点 // 题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该 // 结点. #include <iostream> ...

  9. 【剑指Offer学习】【面试题50:树中两个结点的最低公共祖先】

    题目:求树中两个结点的最低公共祖先,此树不是二叉树,而且没有指向父节点的指针. 树的结点定义 private static class TreeNode { int val; List<Tree ...

随机推荐

  1. 合并两个JsonArray

    //合并两个JSONArray public static String joinJSONArray(JSONArray mData, JSONArray array) { StringBuffer ...

  2. 树莓派无显示器开启ssh的方法

    在boot根目录新建一个名为 ssh 的空文件即可. boot目录所在分区是fat32格式,可以被windows识别和操作 带有系统的tf卡(或SD卡)插入读卡器中,新建ssh文件即可,注意无后缀名

  3. 动态规划之91 decode ways

    题目链接:https://leetcode-cn.com/problems/decode-ways/description/ 参考:https://www.jianshu.com/p/5a604070 ...

  4. 【Python045-魔法方法:属性访问】

    一.属性的几种访问方式 1.类.属性名 >>> class C: def __init__(self): self.x = 'X-man' >>> c = C() ...

  5. uniGUI出新版本了,0.97.0.1081

    uniGUI出新版本了,0.97.0.1081,试用版0.97.0.1075,支持Delphi2006~XE7.下载地址是: http://www.unigui.com/downloads 已在XE6 ...

  6. 【BZOJ4872】分手是祝愿

    分手是祝愿 [题目大意] 有n 个灯,每个灯有两个状态亮和灭,我们用 1 来表示这个灯是亮的,用 0 表示这个灯是灭的,操作第 i 个开关时,所有编号为 i 的约数(包括 1 和 i)的灯的状态都会被 ...

  7. Super-palindrome 【可能是暴力】

    Super-palindrome 时间限制: 1 Sec  内存限制: 128 MB 提交: 486  解决: 166 [提交] [状态] [命题人:admin] 题目描述 You are given ...

  8. centos 编译lantrn

    github上的安装指导: Custom fork of Go is currently required. We'll eventually switch to Go 1.7 which suppo ...

  9. dede的应用

    gbk和utf-8版本选择 gbk是国家编码,所有的内容编码,包括中文,英文,英文字符都占两个字节. utf-8是国际编码,中文三个字节,英文1个字节 现阶段,网站都用gbk编码, 一方面节省本地/网 ...

  10. UVALive 7503 Change(乱搞)题解

    题意:你现在有面额为A的纸币,现在需要面额为B的钱(可以是一张也可以是好多张拼成一张),有一台自动售货机,里面有任意价格的商品,售货机兑换出的零钱是随机的(比如找你0.03可能给你0.01+0.01+ ...