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. 【BZOJ-3553】三叉神经树 树链剖分

    3553: [Shoi2014]三叉神经树 Time Limit: 160 Sec  Memory Limit: 256 MBSubmit: 347  Solved: 112[Submit][Stat ...

  2. hackerrank Similar Pair

    传送门 Problem Statement You are given a tree where each node is labeled from 1 to n. How many similar ...

  3. TAC Beta版本 冲冲冲!!!

    一.Beta版本冲刺博客目录: 第一天 第二天 第三天 第四天 第五天 第六天 第七天 二.Beta版本需要改进完善的功能: service层传入参数的判断与提示以及各函数内的相应提示 界面改进.优化 ...

  4. Publishing failed with multiple errors 异常

    Publishing failed with multiple errors 在使用eclipse发布项目时不能自动生成class文件,且无法启动调试的Tomcat服务.启动过程提示 以上 异常 解决 ...

  5. ionic browser+ios头部高度显示问题

    ionic项目在使用ionic build browser或者打包ios时如果设置头部高度 方法如下 .bar-header { padding:; height:; } .scroll-conten ...

  6. C#之方法的声明与调用

    //params关键字使用 class Program { static void Main(string[] args){ , , , }; Console.WriteLine(AddFunctio ...

  7. 平面内,线与线 两条线找交点 两条线段的位置关系(相交)判定与交点求解 C#

    个人亲自编写.测试,可以正常使用   道理看原文,这里不多说   网上找到的几篇基本都不能用的   C#代码 bool Equal(float f1, float f2) { return (Math ...

  8. struts2的DevMode模式

    在实际应用开发或者是产品部署的时候,对应着两种模式: 1.开发模式(devMode),此时,DevMode=true 2.产品模式(proMode),此时,DevMode=false 在一些服务器或者 ...

  9. MYSQL入门全套(第三部)

    MYSQL入门全套(第一部) MYSQL入门全套(第二部) 索引简介 索引是对数据库表中一个或多个列(例如,employee 表的姓名 (name) 列)的值进行排序的结构.如果想按特定职员的姓来查找 ...

  10. CSS3实现加载中效果

    代码: <!doctype html> <html> <head> <meta charset="utf-8" /> <tit ...