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的更多相关文章

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

  2. LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告

    https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...

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

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

  5. LeetCode Lowest Common Ancestor of a Binary Search Tree (LCA最近公共祖先)

    题意: 给一棵二叉排序树,找p和q的LCA. 思路: 给的是BST(无相同节点),那么每个节点肯定大于左子树中的最大,小于右子树种的最小.根据这个特性,找LCA就简单多了. 分三种情况: (1)p和q ...

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

  7. 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 利用二 ...

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

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

随机推荐

  1. centos 防火墙

    让防火墙放通tcp的 80端口: [root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent 重启防火墙以 ...

  2. 微信签名算法的服务端实现(.net版本)

    一.概要 微信此次开放JS接口,开放了一大批api权限,即使在未认证的订阅号也可以使用图像接口,音频接口,智能接口,地理位置,界面操作,微信扫一扫等功能.要知道:以前订阅号只能接受和被动回复用户消息而 ...

  3. 【bzoj1085】 SCOI2005—骑士精神

    http://www.lydsy.com/JudgeOnline/problem.php?id=1085 (题目链接) 题意 给出一个初始局面,问能否在15步内走到最终局面,并输出最少步数. Solu ...

  4. 【Beta】七天屠蛟记

    团队名字: 一不小心就火了 屠龙天团少年们: 031402504 陈逸超 (组长) 031402505 陈少铭 031402511 黄家俊 031402515 翁祖航 031402516 黄瑞钰 03 ...

  5. <<< 网页中如何利用原生js和jquery储存cookie

    javascript当中的cookie机制,使应用达到了真正的全局变量的要求,cookie是浏览器提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由Java ...

  6. LogStash配置、使用(三)

    LogStash配置 官方文档:https://www.elastic.co/guide/en/logstash/current/index.html 查看yum安装路径 rpm -ql logsta ...

  7. IO流 FileOutputSteam在fos.txt写出hello

    package cn.idcast2; import java.io.FileNotFoundException; import java.io.FileOutputStream; import ja ...

  8. C# 面试知识点总结

    1,事件是对象,委托时类型.事件内部其实就是一个private 的委托和add,remove两个方法. 2.override 和overload的区别: override是对基类中方法的重写,是会覆盖 ...

  9. golang笔记——数组与切片

    一.切片的定义 我们可以从数组(go语言中很少直接使用数组)或者切片来初始化一个新的切片,也可以直接通过 make 来初始化一个所有元素为默认零值的切片. //1.通过数组来初始化切片 arr := ...

  10. Bash 会清空从父进程继承来的 OLDPWD

    即便 Bash 没有从父进程继承任何的环境变量,Bash 自己也会创建三个环境变量,分别是: $ env -i bash -c export declare -x OLDPWD declare -x ...