原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description

描述

给定一棵二叉树,找到两个节点的最近公共父节点(LCA)。

最近公共祖先是两个节点的公共的祖先节点且具有最大深度。

假设给出的两个节点都在树中存在

您在真实的面试中是否遇到过这个题?  是

样例

对于下面这棵二叉树

  4
/ \
3 7
/ \
5 6

LCA(3, 5) = 4

LCA(5, 6) = 7

LCA(6, 7) = 7

标签
二叉树
LintCode 版权所有
 
 
思路:碰到二叉树问题基本上递归。这道题在递去过程中寻找A、B节点,一旦找到停止递归,进行回溯,回溯过程中找到最近公共父节点。
1.递去时,
递归终止条件为:若找到A或者B,当即返回该节点,若找不到返回NULL。
递归式,在左右子树中分别寻找。
2.回溯时,
若A、B在当前根节点左右两侧,那么当前根节点就是A、B的LCA,返回当前根节点;
若A,B在当前根节点的同一侧(left或right),那么另一侧的寻找结果为NULL。寻找结果不为NULL的那侧返回的节点,也就是最先找到的节点,即为A、B的LCA。
 
 
 
AC代码:
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/ class Solution {
public:
/*
* @param root: The root of the binary search tree.
* @param A: A TreeNode in a Binary.
* @param B: A TreeNode in a Binary.
* @return: Return the least common ancestor(LCA) of the two nodes.
*/
TreeNode * lowestCommonAncestor(TreeNode * root, TreeNode * A, TreeNode * B) {
// write your code here
if (root==NULL||A==root||B==root)
{
return root;
}
TreeNode * left=lowestCommonAncestor(root->left,A,B);
TreeNode * right=lowestCommonAncestor(root->right,A,B);
if (left&&right)
{
return root;
}
else if (left)
{
return left;
}
else if(right)
{
return right;
}
else
{
return NULL;
} }
};
参考:
Lowest Common Ancestor  讲解详细,还提供了另外一种计数器的方法
[LeetCode] Lowest Common Ancestor of a Binary Tree系列   总结了系列问题,普通二叉树LCA和二叉搜索树的LCA
 
 
思路2:用DFS求出顶点到A和B的路径,再从两条路径中找到第一个不相等的节点,则上一个节点即为LCA。参考:LintCode-最近公共祖先 
 
AC代码:
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/ class Solution {
public:
/*
* @param root: The root of the binary search tree.
* @param A: A TreeNode in a Binary.
* @param B: A TreeNode in a Binary.
* @return: Return the least common ancestor(LCA) of the two nodes.
*/
TreeNode * lowestCommonAncestor(TreeNode * root, TreeNode * A, TreeNode * B) {
// write your code here
if (root==NULL)
{
return root;
}
vector<TreeNode*> cur;
vector<TreeNode*> pathA;
vector<TreeNode*> pathB;
dfst(cur,root,A,B,pathA,pathB);
TreeNode * result;
for (int i=;i<min(pathA.size(),pathB.size());i++)
{
if (pathA[i]==pathB[i])
{
result=pathA[i];//二者相同,赋值哪个都一样;
}
else
{
break;
}
}
return result;
} void dfst(vector<TreeNode*> cur,TreeNode * root, TreeNode * A, TreeNode * B,vector<TreeNode*> &pathA,vector<TreeNode*> &pathB)
{//注意cur不能加引用,其元素随着递归过程逐渐增多;而pathA和pathB必须为引用,否则值无法传递出去,这两数组相当于返回值;
cur.push_back(root);
if (root==A)
{
pathA=cur;
}
if (root==B)
{
pathB=cur;
}
if (root->left)
{
dfst(cur,root->left,A,B,pathA,pathB);
}
if (root->right)
{
dfst(cur,root->right,A,B,pathA,pathB);
}
}
};

88 Lowest Common Ancestor of a Binary Tree的更多相关文章

  1. 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 利用二 ...

  2. 【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 ...

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

  5. [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. According ...

  6. LeetCode Lowest Common Ancestor of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...

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

  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. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. 巧用android:divider属性设置LinearLayout中元素之间的间隔

    如上图,要想实现3个button线性排列并且使它们的大小相同.间隔相等.而且整体填充满整个linearlayout,我们一般的做法是在每两个button之间放一个固定宽度的view,然后设置butto ...

  2. idea社区版+第一个spring boot项目+增删改查+yml修改端口号

    参考:https://www.cnblogs.com/tanlei-sxs/p/9855071.html 中途出现问题时参考了太多 1.下载idea社区版 2.在settings -> Plug ...

  3. 获取AndroidManifest.xml中的meta-data元素

    android 开发中: 在AndroidManifest.xml中,<meta-data>元素可以作为子元素, 被包含在<activity>.<application& ...

  4. Vue的指令和成员

    目录 Vue的指令和成员 指令 表单 斗篷 条件 循环 成员 计算成员 监听成员 Vue的指令和成员 指令 表单 表单指令一般是和属性指令联合使用的,最常见的就是v-model="变量&qu ...

  5. Math: Fibonacci

    https://www.zhihu.com/question/28062458 http://blog.csdn.net/hikean/article/details/9749391 对于Fibona ...

  6. 记 openSUSE 42.3 升级到Leap 15.0

    先将系统的软件更新到最新版本 sudo zypper update 删除42.3的软件源,若有其他数据源,如nginx的,也需要一并删除,可使用zypper lr -d 来查询 sudo zypper ...

  7. P3718 [AHOI2017初中组]alter

    贪心+二分答案 二分最终答案长度 主要问题在check上 ~~我代码写得巨丑,大家还是不要看我的代码了~~ ------------ 1:当mid大于1的时候,贪心策略是这样的: 当前连续的长度大于m ...

  8. LoadRunner中字符串的操作

    LoadRunner中字符串的操作 LoadRunner中常用的字符串操作函数有:                strcpy(destination_string, source_string); ...

  9. 关于Mysql几周的整理文档

    https://files.cnblogs.com/files/swobble/mysql.rar 内容包括 版本测试(5.5,5.6,5.7) 平台测试(windows所有平台) 文件说明 精简说明 ...

  10. ThinkPHP 删除数据

    ThinkPHP删除数据使用delete方法,例如: 直线电机价格 $Form = M('Form'); $Form->delete(5); 表示删除主键为5的数据,delete方法可以删除单个 ...