// 面试题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. Mysql 按天自动分区,合并老分区

    适用于每天一个分区...不断加分区,导致分区不够用的情况 CREATE DEFINER=hehe@XXXXXX PROCEDURE p_auto_partition_day(IN databaseNa ...

  2. Vue小案例 之 商品管理------添加商品

    进行添加button,以及商品列表的创建 html: <div class="form-btn"> <button>确认添加</button> ...

  3. openstack cloud init set password

    设置代理和password #!/bin/bash cd /home/ubuntu wget otcloud-gateway.bj.intel.com/script.tar.gz ]; then cu ...

  4. oracle12c的日志查看

    查看GI日志:切换到grid用户 查看DB日志:切换到oracle的目录下 执行[oracle@swnode1 ~]$ adrci [oracle@swnode1 ~]$ adrci ADRCI: R ...

  5. python --- 05 字典 集合

    一.字典 可变数据类型 {key:value}形式   查找效率高   key值必须是不可变的数据类型 1.增删改查 1).增    dic["新key"] = "新va ...

  6. 分块读取Blob字段数据(Oracle)

    试过了MSSQL的分块读取Blob字段,又尝试在Oracle下完成,发现还是可行的. 首先建立一个存储过程: create or replace procedure PRO_GET_BLOB(     ...

  7. 终于记住回车和换行cr lf的来由和含义了 -参考: http://www.cnblogs.com/me115/archive/2011/04/27/2030762.html

    回车: carriage return, 是将光标在同一行中, 回到当前行的 行首. 回来的本意就是 返回.. 所以 是同一行的行首. CR 换行: line feed: feed: 饲养(动物); ...

  8. Nodejs学习笔记2

    在linux中, 个人用户的文件, 通常是放在 自己的 家目录中的, root用户放在 /root中. root用户根其他普通用户不同, root用户是专门放在 /root目录中的, 而普通用户的文件 ...

  9. SpringBoot 使用validation数据校验

    后端对数据进行验证 添加包 hibernate-validator <!-- https://mvnrepository.com/artifact/org.hibernate.validator ...

  10. Linux command: grep

    How to use grep to match multiple strings in the same line? grep 'string1\|string2' filename grep -E ...