By given a tree structure, task is to find lowest common ancestor:

For example, LCA(4, 5) --> >3

LCA(4,2) --> 1

LCA(3, 5) --> 3

LCA(6, 6) --> 6

Solution to solve the problem:

Found two path to the given two node, then compare two list to see from which point, they are no long equals:

[4,3,1]
[5,6,3,1] // the lowest common ancestor would be 3

  

Code:

function createNode(val, left = null, right = null) {
return {
val,
left,
addLeft(leftKey) {
return (this.left = leftKey ? createNode(leftKey) : null);
},
right,
addRight(rightKey) {
return (this.right = rightKey ? createNode(rightKey) : null);
}
};
}
function createBT(rootKey) {
const root = createNode(rootKey);
return {
root,
// Lowest Common Ancestor
lca(root, j, k) {
function helper(node, val) {
let leftPath;
let rightPath;
if (!node) {
return null;
} // One we found the value,
// constucte an array, then the path will be added to this array
// thinking JS call stack, from bottom up
// The way recusive works is found the base case, then do the bottom up
if (node.val === val) {
return [val];
} if (node.left) {
// If foudd will return an array
// If not then will return null
leftPath = helper(node.left, val);
if (leftPath !== null) {
leftPath.push(node.val);
return leftPath;
}
} if (node.right) {
// If foudd will return an array
// If not then will return null
rightPath = helper(node.right, val);
if (rightPath !== null) {
rightPath.push(node.val);
return rightPath;
}
} return null;
} const jPath = helper(root, j);
const kPath = helper(root, k);
let found = null; while (jPath.length > && kPath.length > ) {
let fromJ = jPath.pop();
let fromK = kPath.pop();
if (fromJ === fromK) {
found = fromJ;
} else {
break;
}
} return found;
}
};
} const tree = createBT("");
const root = tree.root;
const left = root.addLeft("");
root.addRight("");
const leftleft = left.addLeft("");
const leftright = left.addRight("");
const leftRighttleft = leftright.addLeft(""); console.log(tree.lca(root, "", "")); //
console.log(tree.lca(root, "", "")); //
console.log(tree.lca(root, "", "")); //
console.log(tree.lca(root, "", "")); //

[Algorithm] Tree: Lowest Common Ancestor的更多相关文章

  1. Data Structure Binary Tree: Lowest Common Ancestor in a Binary Tree

    http://www.geeksforgeeks.org/lowest-common-ancestor-binary-tree-set-1/ #include <iostream> #in ...

  2. [LeetCode] 1123. Lowest Common Ancestor of Deepest Leaves 最深叶结点的最小公共父节点

    Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. Recall that: Th ...

  3. [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点

    4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...

  4. [LeetCode] 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 ...

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

  6. 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]

    [题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下:  C++ Code  123456   struct BinaryTreeNode {     int ...

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

  8. 数据结构与算法(1)支线任务4——Lowest Common Ancestor of a Binary Tree

    题目如下:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, fin ...

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

随机推荐

  1. (转)park1.0.0生态圈一览

    转自博客:http://www.tuicool.com/articles/FVBJBjN Spark1.0.0生态圈一览 Spark生态圈,也就是BDAS(伯克利数据分析栈),是伯克利APMLab实验 ...

  2. CentOS系统下中文文件名乱码

    原文来自:http://www.zhukun.net/archives/7434 CentOS系统下中文文件名乱码 2014/09/01Linux运维centos.Linuxbear 从windows ...

  3. 【10.11校内测试】【优先队列(反悔贪心)】【莫队】【stl的应用??离线处理+二分】

    上次做过类似的题,原来这道还要简单些?? 上次那道题是每天可以同时买进卖出,所以用两个优先队列,一个存买进,一个存卖出(供反悔的队列). 这道题实际上用一个就够了???但是不好理解!! 所以我还是用了 ...

  4. 【BZOJ-1396&2865】识别子串&字符串识别 后缀自动机/后缀树组 + 线段树

    1396: 识别子串 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 312  Solved: 193[Submit][Status][Discuss] ...

  5. 原生JS实现一个简单的前端路由(原理)

    说一下前端路由实现的简要原理,以 hash 形式(也可以使用 History API 来处理)为例, 当 url 的 hash 发生变化时,触发 hashchange 注册的回调,回调中去进行不同的操 ...

  6. 0056 Spring MVC如何接收浏览器传递来的请求参数--request--形参--实体类封装

    浏览器总会向服务器传递一些参数,那么Spring MVC如何接收这些参数? 先写个简单的html,向服务器传递一些书籍信息,如下: <!DOCTYPE html> <html> ...

  7. python中的__all__和__slots__

    python两个有趣属性__all__可用于模块导入时限制,如:from module import *此时被导入模块若定义了__all__属性,则只有all内指定的属性.方法.类可被导入~若没定义, ...

  8. 在C#中实现视频播放器

    当我们需要在C#中实现视频播放器的时候,可以使用如下几种方法: 一.使用MediaPlayer ActiveX控件 在C#中支持视屏播放器最简单的方式就是插入MediaPlayer控件了,在WPF中还 ...

  9. HDU 4726 Kia's Calculation(贪心)

    Kia's Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  10. linux下patch命令使用详解---linux打补丁命令

    http://blog.csdn.net/pashanhu6402/article/details/51849354 语 法:patch [-bceEflnNRstTuvZ][-B <备份字首字 ...