题目描述

给定一棵二叉树, 找到该树中两个指定节点的最近公共祖先。

百度百科中最近公共祖先的定义: “对于有根树T的两个结点u、v,最近公共祖先表示一个结点x,满足x是u、v的祖先且x的深度尽可能大。”(一个节点也可以是它自己的祖先

        _______3______
/ \
___5__ ___1__
/ \ / \
6 _2 0 8
/ \
7 4

例如,节点5和节点1的最近公共祖先是节点3;节点5和节点4的最近公共祖先是节点5,因为根据定义,一个节点可以是它自己的祖先。

解题思路

考虑用前序遍历的思想,从根节点开始对左右子树递归遍历,分别记录第一个在其左右子树中找到目的节点的节点,即为最近公共祖先的节点。若该子树中不包含目的节点,则返回NULL。具体步骤如下:

  • 先判断该节点是否是要寻找的两个节点,如果是则返回目的节点;
  • 判断是否为空节点,若是则返回NULL;
  • 递归记录左右子树找到的最近公共祖先节点;
  • 若在左右子树中找到了目的节点,则说明此节点为最近的公共祖先节点,返回此结点;
  • 若找到的两节点中至少一个为空,则返回不为空的节点。注意这里可分为两种情况:
    • 若两节点都为空,说明左右子树均未找到目的节点,返回NULL;
    • 若只有一个节点为空,说明在左右子树中只找到了一个目的节点,返回找到的那个节点。对应题目中一个节点为祖先的情况,此情况中找到的那个节点即为公共祖先

代码

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == p || root == q || root == NULL)
return root;
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if(left && right)
return root;
return left == NULL ? right : left;
}
};

LeetCode 236. 二叉树的最近公共祖先(Lowest Common Ancestor of a Binary Tree)的更多相关文章

  1. [Swift]LeetCode236. 二叉树的最近公共祖先 | Lowest Common Ancestor of a Binary Tree

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

  2. [Swift]LeetCode235. 二叉搜索树的最近公共祖先 | 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 the BS ...

  3. Leetcode之236. Lowest Common Ancestor of a Binary Tree Medium

    236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...

  4. leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree

    https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...

  5. 【刷题-LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

  6. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

  7. 88 Lowest Common Ancestor of a Binary Tree

    原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...

  8. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先

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

  9. Java实现 LeetCode 236 二叉树的最近公共祖先

    236. 二叉树的最近公共祖先 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x ...

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

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

随机推荐

  1. python 高级函数

    高级函数 map 格式:map(func, lt) 说明:接受两个参数,一个函数和一个可迭代对象,返回一个生成器,将func依次作用于lt 示例: l = [1,2,3,4,5]​def double ...

  2. colspan和rowspan

    colspan和rowspan这两个属性用于创建特殊的表格. colspan用来指定单元格横向跨越的列数:colspan就是合并列的,colspan=2的话就是合并两列. rowspan用来指定单元格 ...

  3. 25个免费的jQuery/ JavaScript的图表和图形库

    1.  JS Charts Features Prepare your chart data in XML, JSON or JavaScript Array Create charts in dif ...

  4. SuperMap-WebGL-坐标系及转换说明

    转载自:https://blog.csdn.net/supermapsupport/article/details/89519310 一.坐标系介绍我们先来列举下Cesium中的坐标系:WGS84经纬 ...

  5. contenteditable兼容问题

    正常情况下用contenteditable,IE下有兼容性问题需要将个别字母变成大写的contentEditable. 获取contenteditable的内容 对html进行处理 兼容 chrome ...

  6. fiddler抓包小技巧之自动保存抓包数据(可根据需求过滤)

    首先 选择 如果有别的提示的话,你就按着提示走,就会安装一个编辑器,叫Fiddler Script. 正常情况下,你按着上面操作之后就会出现如下界面: 完事了之后,按下Ctrl+F来查找OnBefor ...

  7. python、第七篇:ORM框架SQLAlchemy

    一 介绍 SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据API执行SQL并获取 ...

  8. Mac安装PHP(Homebrew/php弃用、其他第三方tap也已经弃用或者迁移后的安装配置方案)

    一.前言 看网上很多资料,大多数都是 mac安装php,只需要: brew tap homebrew/php brew install phpXX 安装php扩展只需要: brew install p ...

  9. PAT Basic 1007 素数对猜想 (20 分)

    让我们定义d​n​​为:d​n​​=p​n+1​​−p​n​​,其中p​i​​是第i个素数.显然有d​1​​=1,且对于n>1有d​n​​是偶数.“素数对猜想”认为“存在无穷多对相邻且差为2的素 ...

  10. 有趣的动画swf 小鼠吃豆子

    今天发现一个有趣的动画swf,小鼠吃豆子,呵呵 <object width="240" height="206" data="http://cd ...