题目描述

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

百度百科中最近公共祖先的定义: “对于有根树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. JDBC1

    ---恢复内容开始--- create table `account` ( `id` ), `name` ), `balance` ) ); insert into `account` (`id`, ...

  2. linux yum 安装及卸载

    在Centos中yum安装和卸载软件的使用方法安装方法安装一个软件时yum -y install httpd安装多个相类似的软件时yum -y install httpd*安装多个非类似软件时yum ...

  3. PHP强制修改返回的状态码

    在最后的程序执行完毕之前,加入下列语句,即可实现所有的返回码都为200即使在服务器内部发生错误,会报500情况下只要加上register_shutdown_function函数的处理同样可以实现返回2 ...

  4. Lua语言基本语法~运算符

    Lua 变量 变量在使用前,必须在代码中进行声明,即创建该变量. 编译程序执行代码之前编译器需要知道如何给语句变量开辟存储区,用于存储变量的值. Lua 变量有三种类型:全局变量.局部变量.表中的域. ...

  5. CentOS6.5安装zabbix3.0

    Server端 搭建LAMP(Linux+Apache+Mysql+PHP)环境 1.安装MySQL #安装地址:https://dev.mysql.com/downloads/repo/yum/ y ...

  6. linux下编译安装SDL2和ffmpeg

    首先安装sudo apt-get install libsdl2-dev ./configure --prefix=/tools/SDL2 make && make install $ ...

  7. 也来谈谈SQL SERVER 自定义函数~

    在使用SQL SERVER 数据库的时候,函数大家都应该用过,简单的比如 系统聚合函数 Sum(),Max() 等等.但是一些初学者编写自定义函数的时候,经常问起什么是表值函数,什么是标量值函数. 表 ...

  8. java获取音频文件播放时长

    方法一: 在项目开发过程中,需要获取音视频文件时长.查询资料后发现 JAVE能够完美得到想要的结果,JAVE项目简介如下: The JAVE (Java Audio Video Encoder) li ...

  9. Windows系统下如何卸载干净mysql

    一.在控制面板中卸载mysql软件 二.卸载过后删除C:\Program Files (x86)\MySQL该目录下剩余了所有文件,把mysql文件夹也删了 三.windows+R运行“regedit ...

  10. 如何理解Hibernate的延迟加载机制?

    延迟加载就是并不是在读取的时候就把数据加载进来,而是等到使用时再加载.Hibernate使用了虚拟代理机制实现延迟加载.返回给用户的并不是实体本身,而是实体对象的代理.代理对象在用户调用getter方 ...