235.236. Lowest Common Ancestor of a Binary (Search) Tree -- 最近公共祖先
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 -- 最近公共祖先的更多相关文章
- 【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 ...
- 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 ...
- 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 利用二 ...
- 【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 ...
- 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 ...
- [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.
http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- SQLPlus的两种登录方式的不同效果
Windows 8,Oralce11g,命令行 1.输入“sqlplus”,回车,提示:请输入用户名,输入用户名,回车,提示,请输入口令,输入口令后,回车,报ORA-12560:TNS:协议适配器错误 ...
- 转!!spring @component 详解 默认初始化bean的名字 VNumberTask类 就是 VNumberTask
参考链接:信息来源 今天碰到一个问题,写了一个@Service的bean,类名大致为:CUser xml配置: <context:component-scan base-package=&quo ...
- Network---3694poj(桥与LCA)
题目链接 题意: 有n个电脑1-n,m个连接,由于可能存在一些桥,如果这些桥出现了问题,那么会导致一些电脑之间无法连接, 所以建立链接Q次,每次链接a和b电脑,求链接ab后还存在几个桥: 如果 ...
- uchome四大常用入口文件
一.四大常用入口文件 cp.php 编辑日志.相册.活动等等相关编辑操作基本上都从这个文件入口 do.php 登录.注册.找回密码.相册批量上传.在需要密码的情况 ...
- PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]
1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...
- 安装完C++builder6.0启动的时候总是出现无法将'C:\Program Files\Borland\CBuilder6\Bin\bcb.$$$'重命名为bcb.dro
:兼容性问题 运行前右键属性“兼容性”-尝试不同的兼容性.比如“windows 8”
- mongo增删改查封装(C#)
Framework版本:.Net Framework 4 ConnectionUtil源码参见:https://www.cnblogs.com/threadj/p/10536273.html usin ...
- [one day one question] 部分ios版本 在display: inline-block时候不支持flex
问题描述: 部分ios 在display: inline-block时候不支持flex,这怎么破? 解决方案: so easy,不要使用display: inline-block;就行啦 君生我未生, ...
- Element-UI中Upload上传文件前端缓存处理
Element-UI对于文件上传组件的功能点着重于文件传递到后台处理,所以要求action为必填属性.但是如果需要读取本地文件并在前端直接处理,文件就没有必要传递到后台,比如在本地打开一个JSON文件 ...
- 20145221 《Java程序设计》第二周学习总结
20145221 <Java程序设计>第二周学习总结 教材学习内容总结 第二周内容已在假期完成,详见博客: <Java程序设计>第三章-基础语法 代码调试中的问题和解决过程 第 ...