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. Java面试题全集(上)

    2013年年底的时候,我看到了网上流传的一个叫做<Java面试题大全>的东西,认真的阅读了以后发现里面的很多题目是重复且没有价值的题目,还有不少的参考答案也是错误的,于是我花了半个月时间对 ...

  2. console access jquery--------json

    jq = document.createElement('script');  jq.src = "file:///home/liulqiang/jquery.js";  docu ...

  3. 7.1 - CRM系统

    一.简介 crm 客户关系管理软件 ( Customer Relationship Management ) ( 详细内容 ) stark组件(仿admin组件)( 详细内容 ) rbac组件(基于角 ...

  4. 小米范工具系列之十:小米范SSH批量命令执行工具

    小米范SSH批量命令执行工具的主要功能是自动登录多台机器,并执行指定的命令,比如批量抓取shadow.批量获取系统版本.或者做基线时批量抓取配置等. 此工具使用java 1.8以上版本运行. 界面如下 ...

  5. loki之内存池SmallObj[原创]

    loki库之内存池SmallObj 介绍 loki库的内存池实现主要在文件smallobj中,顾名思义它的优势主要在小对象的分配与释放上,loki库是基于策略的方法实现的,简单的说就是把某个类通过模板 ...

  6. 1.Anaconda安装Tensorflow报错UnicodeDecodeError: 'utf-8' codec can't decode ## invalid start byte的问题之解决

    安装TensorFlow pip install --ignore-installed --upgrade tensorflow 报错: UnicodeDecodeError: 'utf-8' cod ...

  7. 第一个Shader程序

    fx文件: float4x4 matWorld; float Time=1.0f; struct VS_OUTPUT { float4 Pos :POSITION; float4 Color :COL ...

  8. Linux查看系统版本信息

    1.查看操作系统相关信息 uname -a 2.查看正在运行的内核版本信息 cat /proc/version //uname -r 3.查看发行版本号  (适用于所有的linux,包括Redhat. ...

  9. Console 窗口

    Console窗口 记住,即是在GUI程序中你也可以拥有一个Console窗口.----这意味着你可以再GUI程序中使用printf.puts. Console窗口由系统的驱动设备程序负责,即是你的程 ...

  10. 索引查找Java实现

    package 索引查找; import java.util.Scanner; public class IndexSearch { public static long stu[] = { 1080 ...