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 ...
随机推荐
- 使用 Visual Studio 将 ASP.NET Web 应用部署到 Azure
原文地址:https://www.azure.cn/zh-cn/documentation/articles/web-sites-dotnet-get-started 配置新的 Web 项目 下一步是 ...
- 迅雷7 纯净版v7.9.18.4724
http://soft2.xzstatic.com/2016/08/Thunder_7.9.13.4666_NoAD_VIP.exe http://www.downza.cn/soft/192064. ...
- Openstack(七)keystone
官方安装文档:https://docs.openstack.org/ocata/zh_CN/install-guide-rdo/index.html 7.1 keystone简介 Keystone 中 ...
- React package.json详解
概述: 每个项目的根目录下面,一般都有一个package.json文件,定义了这个项目所需要的各种模块,以及项目的配置信息(比如名称.版本.许可证等元数据).npm install命令根据这个配置文件 ...
- 关于使用ubuntu的那些事儿
最近把笔记本的系统由windows改成了ubuntu的最新版系统了,其实改变系统最主要的目的就是希望自己能够快速的学会使用linux系统. 所以这是一篇记录了关于一个ubuntu小白第一次使用ubun ...
- macbook 蓝牙关闭按钮灰色的
PRAM重置:1.关机,拔掉所有外设,只连接电源线2.按开机键,接着马上同时按住 command+option+p+r 键,按住后听到三次dang的开机音后放开3.电脑会自行启动,进到登录页面后点关机 ...
- 服务器相关 HTTP 请求错误
HTTP 400 - 请求无效 HTTP 401.1 - 未授权:登录失败 HTTP 401.2 - 未授权:服务器配置问题导致登录失败 HTTP 401.3 - ACL 禁止访问资源 HTTP 40 ...
- 004-notepad++安装。
1.下载地址. 官网:https://notepad-plus-plus.org/ 2.安装.
- C题:A Water Problem(dp||搜索)
原题链接 解法一:递归 #include<cstdio> #include<algorithm> using namespace std; long long n,x,y; l ...
- shoes的安装前后(一)
最近看到一个模型用到了shoes,准备自己试一试.搞了半天,也安装不成功.直接安装包,gem install shoes,失败, 从rubygems上下载最新版本的shoes 然后安装,成功了.随便写 ...