[LeetCode]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.
Subscribe to see which companies asked this question.
简单题,不过加上思考和编码的时间也大概有半小时了吧。。。
思路就是使用dfs,分别dfs 寻找两个数,记录下从root 到目标node 经过了哪些node,如果在某一次dfs 中发现了存在的node 就说明那个node就是LCA。关键在于,要在递归后检查,而不是在递归前,这样才能保证是lowest 的,因为整个检查过程是倒桩的。如果是在递归前检查,就变成最高祖先了(root)。
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
var lowestCommonAncestor = function(root, p, q) {
var _lca = null;
var _set = {};
function dfs(node, a, set) {
if (!node) return;
if (node.val == a) {
if (set[node.val] != void 0 && _lca === null) _lca = node;
else set[node.val] = 1;
return a;
}
var l_find = dfs(node.left, a, set);
if (l_find == a) {
if (set[node.val] != void 0 && _lca === null) _lca = node;
else set[node.val] = 1;
return a;
}
var r_find = dfs(node.right, a, set);
if (r_find == a) {
if (set[node.val] != void 0 && _lca === null) _lca = node;
else set[node.val] = 1;
return a;
}
} dfs(root, p.val, _set);
dfs(root, q.val, _set); return _lca;
};
[LeetCode]Lowest Common Ancestor of a Binary Search Tree的更多相关文章
- [LeetCode] 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: Lowest Common Ancestor of a Binary Search Tree 解题报告
https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...
- LeetCode——Lowest Common Ancestor of a Binary Search Tree
Description: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given no ...
- Python3解leetcode 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 ...
- LeetCode Lowest Common Ancestor of a Binary Search Tree (LCA最近公共祖先)
题意: 给一棵二叉排序树,找p和q的LCA. 思路: 给的是BST(无相同节点),那么每个节点肯定大于左子树中的最大,小于右子树种的最小.根据这个特性,找LCA就简单多了. 分三种情况: (1)p和q ...
- 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 ...
- 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 ...
随机推荐
- TCP协议中的三次握手和四次挥手(图解)
建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 先来看看如何建立连接的. 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资 ...
- hihoCoder 后缀自动机三·重复旋律6
后缀自动机三·重复旋律6 时间限制:15000ms 单点时限:3000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数列. 现在小Hi ...
- iOS播放器 - AVAudioPlayer
今天记录一下AVAudioPlayer,这个播放器类苹果提供了一些代理方法,主要用来播放本地音频. 其实也可以用来播放网络音频,只不过是将整个网络文件下载下来而已,在实际开发中会比较耗费流量不做推荐. ...
- 机器学习笔记—svm算法(上)
本文申明:本文原创,如转载请注明原文出处. 引言:上一篇我们讲到了logistic回归,今天我们来说一说与其很相似的svm算法,当然问题的讨论还是在线性可分的基础下讨论的. 很多人说svm是目前最好的 ...
- 使用U盘重装电脑操作系统
1. 打开360软件管家,找一个软件"蚂蚁U盘启动"下载,下载好以后打开,电脑上插入U盘,我们制作一个启动U盘备用! 图片图片 按提示制作好启动盘后,保管好U盘. 找一个系统下载网 ...
- A Quick Introduction to Linux Policy Routing
A Quick Introduction to Linux Policy Routing 29 May 2013 In this post, I’m going to introduce you to ...
- 转-ArcGIS Engine中的License设置
AE开发中的License有两种方法进行设置,一种是通过LicenseControl控件,另一种是通过IAoInitialize接口设置.整个应用程序中只能有一种方式存在,如果进行了两种License ...
- UI第十五节——UIWebView
- (void)viewDidLoad { [super viewDidLoad]; UIWebView *webView = [[UIWebView alloc] initWithFra ...
- Maven 入门 (1)—— 安装
Maven 入门 (1)—— 安装 http://blog.csdn.net/kakashi8841/article/details/17371837 1.下载maven安装包 http://mave ...
- JS常见错误和分析
列举一些在前端开发中遇到的一些错误信息和解决办法 错误信息 解决办法 Uncaught SyntaxError: Unexpected token o 使传入的字符串不是符合严格的JSON格式会抛出异 ...