1,题目要求

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]

Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.

Example 2:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

Note:

  • All of the nodes’ values will be unique.
  • p and q are different and both values will exist in the binary tree.

给定二叉树,找到树中两个给定节点的最低共同祖先(LCA)。

根据维基百科上LCA的定义:“最低共同祖先在两个节点p和q之间定义为T中的最低节点,其中p和q都是后代(我们允许节点成为其自身的后代)。”

2,题目思路

对于这道题,是寻找两个节点在树中的最低共同祖先。

在解决上,我们可以使用递归的策略,分别在子树中寻找节点p和节点q。

对于一个节点root:

  • 如果它的左右子树中都没有p和q,就说明它不是二者的祖先。

  • 如果p和q分别在root的左右子树/右左子树中,说明root就是p和q的祖先,而且应该也是最低公共祖先。

  • 如果在左子树中没有搜到p或者q,但是右子树搜到了,就说明root->right是p或者q的祖先。右子树同理。

3,代码实现

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/int x = []() {ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);return 0;
}();class Solution {public:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if(root == nullptr || root == p || root == q)return root;TreeNode* left = lowestCommonAncestor(root->left, p ,q);TreeNode* right = lowestCommonAncestor(root->right, p, q);if(left == nullptr && right == nullptr)return nullptr;if(left != nullptr && right != nullptr)return root;return left == nullptr? right : left;}
};

Lowest Common Ancestor of a Binary Tree相关推荐

  1. 236 Lowest Common Ancestor of a Binary Tree

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

  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. 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. 思路 这一次 ...

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

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

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

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

  6. 二叉树:最近的公共祖先 Lowest Common Ancestor of a Binary Tree

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

  7. 236. Lowest Common Ancestor of a Binary Tree

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

  8. 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 ...

  9. Lowest Common Ancestor of a Binary Search Tree a Binary Tree

    235. Lowest Common Ancestor of a Binary Search Tree 题目链接:https://leetcode.com/problems/lowest-common ...

  10. [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点

    4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...

最新文章

  1. H标签对网站SEO优化是不可或缺的!
  2. git 入门教程之协同开发
  3. Ubuntu 16.04 安装JDK
  4. python123第三章作业答案_swift playground类似的游戏
  5. ⼤海捞针 —— Scan
  6. CSS3:FlexBox的详解
  7. Java,C++四舍五入
  8. “我不是个优秀的 Web 开发人员,我只是擅长搜索谷歌”
  9. 话费充值哪里便宜?这样充帮我省了不少钱,推荐给您
  10. CodeRunner激活
  11. python中xlrd模块的用法_用xlrd模块读取合并单元格(merged cell)
  12. 计算机链接投影仪后不显示桌面,win10系统连接投影后不显示桌面图标怎么办
  13. 机动车c1科三考试语言灯光,史上最全科目三灯光模拟图解
  14. mysql_assoc函数_PHP:MySQL函数mysql_fetch_assoc()的用法
  15. ERP开发的一些闲话—之一
  16. android 六边形布局,Android自定义View——一个可定制的六边形阵列
  17. 什么是重排和重绘?何时会触发?
  18. Unity中实现赛车游戏
  19. 汽车通信协议:一文搞懂Flexray通信
  20. 虚拟现实技术实现理论之梦境论述

热门文章

  1. PHP显示了验证码但不能登陆,thinkphp5 登陆后台验证码无法显示
  2. Arcgis实例学习5--统计直方图、空间分布图、统计信息
  3. 历史大数据证“低薪薄俸”易诱发腐败
  4. 基于单片机的无线防盗报警系统设计(#0449)
  5. 熬夜淦了近 3W 字的 Docker 教程,从入门到精通(建议收藏)
  6. 计算机主机不启动但 主机闪,电脑主机电源灯闪烁无法启动不了
  7. 多因子融合的实体识别与链指消歧
  8. 梳理企业业务流程四步法
  9. Ubuntu | ubuntu下安装edge
  10. Ubuntu安装中文输入法