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.

思路:

  通过先序遍历,分别找到要查找结点(5 , 4)的路径(3->5 , 3->5->2->4),然后求路径序列中最后一个公共结点(5)即为所求。

C++:

 /**
* 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 {
private:
vector<TreeNode* > plist; public:
void rec(TreeNode *root, TreeNode *tar, vector<TreeNode* >& res)
{
if(root == tar)
{
vector<TreeNode* >::iterator it = plist.begin();
for(; it != plist.end(); it++)
res.push_back(*it); return ;
} if(root->left != )
{
plist.push_back(root->left);
rec(root->left, tar, res);
plist.pop_back();
} if(root->right != )
{
plist.push_back(root->right);
rec(root->right, tar, res);
plist.pop_back();
}
} TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == )
return ; vector<TreeNode* > list1, list2; plist.push_back(root);
rec(root, p, list1); plist.clear();
plist.push_back(root);
rec(root, q, list2); TreeNode *ret = ;
vector<TreeNode* >::iterator it1 = list1.begin();
vector<TreeNode* >::iterator it2 = list2.begin();
for(; it1 != list1.end() && it2 != list2.end(); it1++, it2++)
{
if(*it1 == *it2)
ret = *it1;
} return ret;
}
};

【LeetCode 236】Lowest Common Ancestor of a Binary Tree的更多相关文章

  1. 【LeetCode 235】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 ...

  2. 【树】Lowest Common Ancestor of a Binary Tree(递归)

    题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accor ...

  3. LeetCode OJ: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 ...

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

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

  8. leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree

    leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...

  9. 88 Lowest Common Ancestor of a Binary Tree

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

随机推荐

  1. 打印TMemo的内容到打印机

    Canvas.TextOut真是好用,Printer也实在好用: procedure PrintTStrings(Lst : TStrings) ; var I, Line : Integer; be ...

  2. CentOS软件安装目录查找

    注:一般的软件的默认安装目录在/usr/local或者/opt里,可以到那里去找找. 指令名称:whereis 功能介绍:在特定目录中查找符合条件的文件.这些文件的烈性应属于原始代码,二进制文件,或是 ...

  3. (CentOS) 程序安装包管理:yum

    简介: Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的服务器 ...

  4. js学习之道:表单验证公共js

    /** 文件名称:CommonUtil.js 作者  : Yuce 编制时间: 2010-03-24 文件内容:一些常用的js公用类.工具类 包括方法: g_FormFieldIsNull    判断 ...

  5. 《c程序设计语言》读书笔记--大写转小写

    #include <stdio.h> #include <stdlib.h> #include <string.h> int aoti(char c) { if(c ...

  6. 38-语言入门-38-Coin Test

    题目地址: http://acm.nyist.net/JudgeOnline/problem.php?pid=204    描述As is known to all,if you throw a co ...

  7. 2-Medium下的MultipleCommandAssembly

    1.首先创建一个控制台项目 2.添加引用[红色的都是将项目添加为引用,其中蓝色的log4net是直接从源代码下的Reference文件夹下引用的dll] 3.写代码 1)首先将config配置好[ht ...

  8. JAVA设计模式之【工厂方法模式】

    看例子 1.TV产品接口,负责播放 public interface TV // TV接口 { public void play(); } 2.TV工厂接口,负责生产产品 public interfa ...

  9. 了解Objective-C中NSAutoreleasePool使用方法

    本文的目的是来了解Objective-C中NSAutoreleasePool使用方法,Objective-C的Foundation库实际上是种运行级对象系统,与一般的对象语言,例如C++,Java不一 ...

  10. App开放接口api安全性—Token签名sign的设计与实现

    前言 在app开放接口api的设计中,避免不了的就是安全性问题,因为大多数接口涉及到用户的个人信息以及一些敏感的数据,所以对这些接口需要进行身份的认证,那么这就需要用户提供一些信息,比如用户名密码等, ...