已知二叉树,求二叉树中给定的两个节点的最近公共祖先。 最近公共祖先: 两节点v与w的最近公共祖先u,满足在树上最低(离根最 远),且v,w两个节点都是u的子孙。


如上二叉树,6和8号节点的公共祖先有4,1;但是最近的公共祖先仅为4

这里我们发现,想要快速获取两个节点的最近的公共祖先节点,首先需要知道两个节点各自从根节点到当前的路径,即:
6号节点的路径为:[1,4,6]
8号节点的路径为:[1,4,5,8]

实现如下:

/*先序遍历查找路径*/
void getPathToNode(Tree *root, Tree *search,  //root根节点,search为当前节点std::vector<Tree *> &path, //存储临时路径std::vector<Tree *> &result, //存储最终结果int &finish) { //是否找到的状态if (!root || finish ) { //如果找到或者根节点为空,则返回空return;}path.push_back(root); if (root == search) { //发现当前节点为我们要找的路径finish = 1;result = path; //将路径列表给予最终的结果}/*继续后序左右子树的查找*/getPathToNode(root -> left, search, path, result, finish);getPathToNode(root -> right, search, path, result, finish);/*发现没有找到,则将当前节点pop*/result.pop_back();
}

当我们能够知道两个节点的路径,那么接下来的祖先节点就非常好找了,实现如下:

Tree *getPublicNode(Tree *root, Tree *p, Tree *q) {if (root == NULL) {return NULL;}std::vector<Tree *> p_path;//p的最终路径std::vector<Tree *> q_path;//q节点的最终路径std::vector<Tree *> path;//存储临时路径int finish = 0;getPathToNode(root, p, path, p_path,finish);path.clear();finish = 0;getPathToNode(root, q, path, q_path,finish);int path_len = 0;if (p_path.size() > q_path.size()) { //需要取一个最短的路径进行后序的遍历path_len = q_path.size();}else {path_len = p_path.size();}Tree *result;for (int i = 0;i < path_len; ++i) { //从开始(根节点)遍历到叶子节点,越靠后的节点越为最近的公共祖先节点if (p_path[i] == q_path[i]) {result = p_path[i];}}return result;
}

二叉树:最近的公共祖先 Lowest Common Ancestor of a Binary Tree相关推荐

  1. LeetCode:235. 二叉搜索树的最近公共祖先(Lowest Common Ancestor of a Binary Search Tree)

    二叉搜索树性质: 1.任意节点node,其左子树中的val不大于node.val,其右子树中的val不小于node.val. 2.不同的二叉搜索树可以代表同一组值的集合 3.二叉搜索树的基本操作和树的 ...

  2. 22 最近共同先祖(Lowest Common Ancestor of a Binary Tree)

    文章目录 1 题目 2 解决方案 2.1 思路 2.2 图解 2.3 时间复杂度 2.4 空间复杂度 3 源码 3.1 遍历法 1 题目 题目:最近共同先祖(Lowest Common Ancesto ...

  3. 236 Lowest Common Ancestor of a Binary Tree

    题目: 236 Lowest Common Ancestor of a Binary Tree 这道题和 235 基本一样 class Solution:# @param {TreeNode} roo ...

  4. leetcode——Lowest Common Ancestor of a Binary Tree

    题目 Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. 思路 这一次 ...

  5. 最近公共祖先 (Lowest common ancestor)

    问题: 给定一个二叉树,找到两个节点NA, NB的最近公共祖先(LCA). 比如对于下图,4 和 7 的 LCA 是6, 1和13的LCA 是 8. 分析: 我们这里先考虑一般的二叉树(BT),然后再 ...

  6. LeetCode Lowest Common Ancestor of a Binary Tree(LCA问题)

    问题:求二叉树中两个结点p,q的最近公共祖先 思路:第一种方法是二叉树的递归,当搜索是当前结点为p或者为q时,直接返回对应结点.然后再左右子树的返回情况 1.如果左右子树非空,则当前结点就是要找的最近 ...

  7. leetcode 236. Lowest Common Ancestor of a Binary Tree | 236. 二叉树的最近公共祖先(Java)

    题目 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题解 思路来源:左程云<程序员代码面试指南&g ...

  8. 236. Lowest Common Ancestor of a Binary Tree

    原题链接:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/ 代码实现如下: impo ...

  9. Lowest Common Ancestor of a Binary Search Tree(树中两个结点的最低公共祖先)

    题目描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...

最新文章

  1. Oracle中的iot_type,oracle IOT表学习
  2. 如何画正太分布曲线_图解统计学 01 | 神奇的正态分布
  3. Elasticsearch修改字段之别名,扩展数据迁移
  4. Java中Xml文件的解析
  5. Nutch1.2二次开发详细攻略(二)【图文】------Windows平台下Nutch1.2的搭建
  6. 基于eNSP的校园网设计的仿真模拟
  7. dell延长对显卡的保修服务
  8. android 连接已保存的wifi,手机连接WiFi显示已保存但是连接不上
  9. iphone科学计算器使用
  10. RecyclerView实现广告轮播图(一)
  11. 202. 快乐数 (Python 实现)
  12. 微信useragent java_微信内置浏览器和小程序的 User Agent 区别及判断方法
  13. prgrmz的作品集
  14. 短信(SMS)的解释分类以及原理
  15. 关于微程序、微指令和微命令
  16. css制作导航栏的三角形
  17. IDE创建vue项目
  18. Navicat Premium 15 无法导出excel格式的文件
  19. 【库存控制技术】方法
  20. 基于GOES-16图像的夜间海雾检测

热门文章

  1. 运算符优先级记忆口诀及列表(转)
  2. Servlet生命周期与工作原理
  3. java card applet_可多选的javacard applet | 学步园
  4. 如何定义带有可选参数的函数python_Python如何定义有可选参数的元类
  5. 4 关卡编辑器_虚幻引擎4与生存游戏产生化学反应,超真实开放世界手游诞生
  6. /xiaolei.php|martin_十步解决Php Utf-8编码(转贴)
  7. c语言fread无法存储,【求助】C语言fread读取二进制文件时,读取结果全都是零
  8. ssm 异常捕获 统一处理_SSM 统一异常处理
  9. consul服务下线通知_Consul微服务的配置中心体验篇-阿里云开发者社区
  10. php mysql备份脚本_MySQL备份脚本,mysql脚本