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 v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

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

For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

Subscribe to see which companies asked this question

非递归实现:

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == nullptr)
return root;
stack<TreeNode*> sta;
vector<TreeNode*> vec;
bool tag1 = false;
bool tag2 = false;
sta.push(root);
TreeNode* lastRoot = root;
while (!sta.empty())
{
root = sta.top();
if (root == p) {
if(tag1 == false && tag2 == false)
vec.push_back(root);
tag1 = true;
}
else if (root == q) {
if (tag2 == false && tag1 == false)
vec.push_back(root);
tag2 = true;
}
if (!tag1 && !tag2)
vec.push_back(root);
if (tag1 && tag2 && find(vec.begin(), vec.end(), root) != vec.end())
return root; if (lastRoot != root->right)
{
if (lastRoot != root->left) {
if (root->left != nullptr) {
sta.push(root->left);
continue;
}
}
if (root->right != nullptr) {
sta.push(root->right);
continue;
}
}
lastRoot = root;
sta.pop();
}
return nullptr;
}

递归实现:

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (!root || root == p || root == q) return root;
TreeNode* l = lowestCommonAncestor(root->left, p, q);
TreeNode* r = lowestCommonAncestor(root->right, p, q);
return l && r ? root : l ? l : r;
}

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

  1. Lowest Common Ancestor of a Binary Tree——Leetcode

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

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

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

  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】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. 88 Lowest Common Ancestor of a Binary Tree

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

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

  8. LeetCode Lowest Common Ancestor of a Binary Tree

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

  9. [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. VAST3.0规范

    VAST3.0视频广告投放规范 Posted on 2014年2月15日 1.术语 随着视频广告行业的发展,某些术语已经得到了广泛的采用.以下定义该文档中与视频广告投放相关的一些术语: 广告荚(Ad ...

  2. 利用apache的mod_rewrite做URL规则重写

    使用mod_rewrite做url重写,伪静态,做过很多次,这次用几个例子记下来,便于后面查用. 使用方法: 1.在conf目录的httpd.conf文件中找到: LoadModule rewrite ...

  3. iOS 容器控制器 (Container View Controller)

    iOS 容器控制器 (Container View Controller) 一个控制器包含其他一个或多个控制器,前者为容器控制器 (Container View Controller),后者为子控制器 ...

  4. HDU 2080 夹角有多大II

    夹角有多大II Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  5. 关于ionic开发的一些总结(项目启动设置,app图标名称更改)

    1.更改包名称 一般项目刚生成时,ionic会根据项目名称自动给你生成一个包名.要修改包名的话,在根目录的config配置文件中,<widget id="x.x.x"换成自己 ...

  6. java 继承的学习(转)

    转自:http://www.cnblogs.com/happyframework/p/3332243.html,非常感谢啊 public class test { /** * @param args ...

  7. java静态初始化代码块

    /* * 为什么Java中为什么没有静态构造函数.其实Java中不叫静态构造函数,称作静态初始化,或者静态代码块. * 可以通过这样的代码实现相同的功能: */ public class test { ...

  8. web前端面试题及答案

    1.常用那几种浏览器测试?有哪些内核(Layout Engine)? 答: (Q1) 浏览器:IE,Chrome,FireFox,Safari,Opera.    (Q2) 内核:Trident,Ge ...

  9. Android 获取浏览器当前分享页面的截屏

    Android 获取浏览器当前分享页面的截屏 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/BrowserScreenShotActivity 文中 ...

  10. iframe的优缺点?

    1.`<iframe>`优点: 解决加载缓慢的第三方内容如图标和广告等的加载问题 Security sandbox 并行加载脚本 2.`<iframe>`的缺点: *ifram ...