[Algorithm] Tree: Lowest Common Ancestor
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的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]
[题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下: C++ Code 123456 struct BinaryTreeNode { int ...
- [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 ...
- 数据结构与算法(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 ...
- 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 ...
随机推荐
- OI回忆录(流水账)
今天是NOI2017疏散日,晚上回到家在电脑前写下这篇回忆录. 初中参加过几次普及组,拿过一等奖,自招进slyz. 高一参加过CTSC2016,狗牌滚粗qwq 高二参加过WC2017,CTSC2017 ...
- 2013年JavaScript开发人员调查结果
JavaScript开发人员调查现在已经结束,一如既往社区对结果进行了进一步分析: 总结(汉语) 原始数据(电子表格) 2012年结果 51%的被参与者写客户端代码,而28%的人说他们编写服务器端代码 ...
- OpenGL ES 3.0 帧缓冲区对象基础知识
最近在帧缓冲区对象这里卡了一下,不过前面已经了解了相关的OpenGL ES的知识,现在再去了解就感觉轻松多了.现在就进行总结. 基础知识 我们知道,在应用程序调用任何的OpenGL ES命令之前,需要 ...
- MySQL规约(阿里巴巴)
建表规约 [强制]表达是与否概念的字段,必须使用 is _ xxx 的方式命名,数据类型是 unsigned tinyint ( 1 表示是,0 表示否 ) ,此规则同样适用于 odps 建表. 说明 ...
- C#高级编程9-目录
C#高级编程 ===================================================== .NET体系结构 核心C# 对象与类型 继承 泛型 数组 运算符和类型强制转换 ...
- java值和地址值传递、字符串常量池的理解
#java值和地址值传递的理解: - 基本数据类型和基本数据类型的封装类都是:值传递 * 形式参数的改变不会影响实际参数的改变(相当于将值复制一份传递给形参,自身没做任何改变) - 引用数据 ...
- spring---transaction(5)---事务的体系
1.写在前面 事务的模型为3中: 本地事务模式. 编程事务模式. 声明事务模式. 例子1:本地事务模式 Connection conn=jdbcDao.getConnection(); Prepare ...
- Notepad++源代码阅读——窗口元素组织与布局
1.1 前言 这两天在看notepad++ 1.0版本的源代码.看了许久终于把程序的窗口之间的关系搞清楚了现在把其组织的要点写于此,希望对大家有所帮助. 1.2 窗口元素之间的关系 Notepad++ ...
- ASK,OOK,FSK,GFSK是什么
http://www.21say.com/askookfskgfsk%E6%98%AF%E4%BB%80%E4%B9%88/ ASK是幅移键控调制的简写,例如二进制的,把二进制符号0和1分别用不同的幅 ...
- svn如何提取文件更新列表
eclipse svn插件site-1.10.1 Slik-Subversion-1.8.0-x64.msi ---可以使用svn命令,如svn status 显示修改过的本地文件,如下示例: I: ...