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

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

        _______6______
/ \
___2__ ___8__
/ \ / \
0 _4 7 9
/ \
3 5

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

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
while(root) {
if (p->val > root->val && q->val > root->val) {
root = root->right;
continue;
}
if (p->val < root->val && q->val < root->val) {
root = root->left;
continue;
}
return root;
}
return NULL;
}

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

(1)一次递归找两个节点

TreeNode* lowestCommonAncestor02(TreeNode* root, TreeNode* p, TreeNode* q) {

    //return if found or not found, return NULL if not found
if (root==NULL || root == p || root == q) return root; //find the LCA in left tree
TreeNode* left = lowestCommonAncestor02(root->left, p, q);
//find the LCA in right tree
TreeNode* right = lowestCommonAncestor02(root->right, p, q); //left==NULL means both `p` and `q` are not found in left tree.
if (left==NULL) return right;
//right==NULL means both `p` and `q` are not found in right tree.
if (right==NULL) return left;
// left!=NULL && right !=NULL, which means `p` & `q` are seperated in left and right tree.
return root;
}

(2)分别记录根节点到所求节点的先序遍历路径,返回路径中最后一个相同的节点

bool findPath(TreeNode* root, TreeNode* p, vector<TreeNode*>& path) {
if (root==NULL) return false;
if (root == p) {
path.push_back(p);
return true;
} path.push_back(root);
if (findPath(root->left, p, path)) return true;
if (findPath(root->right, p, path)) return true;
path.pop_back(); return false;
} //Ordinary way, find the path and comapre the path.
TreeNode* lowestCommonAncestor01(TreeNode* root, TreeNode* p, TreeNode* q) { vector<TreeNode*> path1, path2; if (!findPath(root, p, path1)) return NULL;
if (!findPath(root, q, path2)) return NULL; int len = path1.size() < path2.size() ? path1.size() : path2.size();
TreeNode* result = root;
for(int i=; i<len; i++) {
if (path1[i] != path2[i]) {
return result;
}
result = path1[i];
}
return result;
}

235.236. Lowest Common Ancestor of a Binary (Search) 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. LeetCode (236):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 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】235. Lowest Common Ancestor of a Binary Search Tree (2 solutions)

    Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest com ...

  5. LeetCode_235. Lowest Common Ancestor of a Binary Search Tree

    235. Lowest Common Ancestor of a Binary Search Tree Easy Given a binary search tree (BST), find the ...

  6. [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.

    http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...

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

  8. Lowest Common Ancestor of a Binary Search Tree、Lowest Common Ancestor of a Binary Search Tree

    1.Lowest Common Ancestor of a Binary Search Tree Total Accepted: 42225 Total Submissions: 111243 Dif ...

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

随机推荐

  1. ClassicLink互通原理

    ClassicLink概述_ClassicLink_用户指南_专有网络 VPC-阿里云 https://help.aliyun.com/document_detail/65412.html Class ...

  2. JavaCollection Java 集合框架

    Spring Injecting Collection https://www.tutorialspoint.com/spring/spring_injecting_collection.htm No ...

  3. gnome,xfce,unity,vncserver chinese,jvm locale language

      __________________________ yum search vnc-server sudo yum install tigervnc-server vncserver -list ...

  4. Django - 项目总结

    总结: 基础,进阶,项目 Django - 练习(图书管理系统) 前后端分离得项目: vue + rest framework 项目: 图书增删改查页面 BBS + BLOG系统 CRM系统   在线 ...

  5. yii2框架2 (二)项目结构

    原文 http://www.yiichina.com/doc/guide/2.0/structure-overview 应用结构 应用中最重要的目录和文件(假设应用根目录是 basic): basic ...

  6. PAT 1046 Shortest Distance[环形][比较]

    1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...

  7. log4j2介绍及配置

    一.log4j2概述 在日常的开发,测试和生产环境中,日志记录了应用,服务运行过程中的关键信息,以及出现异常时的堆栈,这些信息常常作为查询,定位,解决问题的关键,因此在任何系统中,对日志的使用得当,将 ...

  8. ELK日志分析工具

    一.ELK介绍 1.1 elasticsearch 1.1.1 elasticsearch介绍 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎 ...

  9. Java用数据结构解决实现问题之数学问题

    有趣的整数: 完数:如果一个数字恰好等于他的因子之和,就叫做完数,需求是求出10000以内的所有的完数. 解法:1.用n去除以1-n之间的所有的整数,将能整除的被除数保存到一个数组中,作为n的一个因子 ...

  10. Scrapy:学习笔记(2)——Scrapy项目

    Scrapy:学习笔记(2)——Scrapy项目 1.创建项目 创建一个Scrapy项目,并将其命名为“demo” scrapy startproject demo cd demo 稍等片刻后,Scr ...