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. 解决mysql 1864 主从错误

     从字面意思看了一下是因为slave_pending_jobs_size_max默认值为16777216(16MB),但是slave接收到的slave_pending_jobs_size_max为 ...

  2. CFD-post的奇技淫巧

    此处记录两个后处理美化的技巧:1.关于contour显示的美化:2.关于legend的显示美化 1. 直接举例说明,现在cfd-post里导入了一个二维case,先建立一个plane: apply以后 ...

  3. Target runtime Apache Tomcat v8.0 is not defined.

    Target runtime Apache Tomcat v8.0 is not defined. Window-Preference-MyEclipse-Targeted Runtimes,选择存在 ...

  4. sass纯新手(一)

    说是教程还真是有点不敢当,只是将自己今天上手sass的流程给记录下来,给一些和我一样的小白菜一点参考而已,照着走一遍应该就会对sass有基本的认识了,也请大神们不吝赐教. 很久之前同事做了一个关于sa ...

  5. 蓝桥网试题 java 基础练习 分解质因数

    -------------------------------------------------------------------------- 递归更多的用在多分支情况中 本题用循环就可以了 用 ...

  6. 详谈Struts2

    介绍struts2: struts2是一个基于mvc设计模式的web层框架. 详谈struts2的执行流程: struts2的执行流程:用户发送请求---->首先经过Struts2的核心过滤器- ...

  7. 蓝牙连接音响问题(android电视)

    最近老大让我开发电视的蓝牙,由于android电视的蓝牙不稳定和设计上的各种各样的要求,需要在原有的基础上做一些更改,中间遇到了各种问题,在此总结一下. 我们首先要获取blueToothAdapter ...

  8. 睡不着,复习一下C++基础中的基础(深拷贝与浅拷贝)

    #include <iostream> #include <string> #include <assert.h> using namespace std; //声 ...

  9. .net 基础服务开源战略规划备忘录

    公司现状 1. 技术人员水平限制: 基础研发人员技术细节,性能处理能力不足,技术视野不够开阔;甚至一些高可用,高性能方案的概念都未听闻,更别提发展方向和思路了,令人痛心. 2. 技术反馈渠道限制: 公 ...

  10. Robot Framework的安装

    一.安装环境:Windows 64位操作系统64位2.7版本Python 二.简要步骤:1. 安装Python(RF是基于python 的,所以需要有python环境):2. 安装wxPython ( ...