[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 ...
随机推荐
- luoguP4571 [JSOI2009]瓶子和燃料 裴蜀定理
裴蜀定理的扩展 最后返回的一定是\(k\)个数的\(gcd\) 因此对于每个数暴力分解因子统计即可 #include <map> #include <cstdio> #incl ...
- bzoj4399 魔法少女LJJ 线段树合并
只看题面绝对做不出系列.... 注意到\(c \leqslant 7\),因此不会有删边操作(那样例删边干嘛) 注意到\(2, 5\)操作十分的有趣,启示我们拿线段树合并来做 操作\(7\)很好处理 ...
- XP下安装Centos 6.4 双系统 :Linux系统分区及挂载点,关键引导程序启动设置
一.关于Linux的分区情况 虽然硬盘分区表中最多能存储四个分区,但我们实际使用时一般只分为两个分区,一个是主分区(Primary Partion)一个是扩展分区(extended partition ...
- TF-timeline的使用经验记录
timeline的使用经验记录:https://towardsdatascience.com/howto-profile-tensorflow-1a49fb18073d 看了TF-summit2018 ...
- web文件上传组件比较jQuery File Upload和Fine Uploader
jQuery File Upload: https://blueimp.github.io/jQuery-File-Upload/ Fine Uploader: http://fineuploader ...
- Problem H: 深入浅出学算法009-韩信点兵
Description 秦朝末年,楚汉相争.有一次,韩信将1500名将士与楚王大将李锋交战.苦战一场,楚军不敌,败退回营,汉军也死伤四五百人,于是,韩信整顿兵马也返回大本营.当行至一山坡,忽有后军来报 ...
- python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐(二)
在上一篇blog:python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 中介绍了python中的tkinter的一些东西,你可能对tkinter有一定的了解了.这篇b ...
- php模块组成
php总共有三个模块:内核.ZEND引擎.扩展. 内核是用来处理请求.文件流.错误处理等操作的: ZEND引擎是将源文件转换成机器语言,然后在虚拟机上运行: 扩展层是一组函数.类库和流,php使用它们 ...
- MySql篇
CentOS6下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.26. 一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server rpm -qa | ...
- DEDEcms和帝国cms的几点比较
前言:最近有很多人问我DEDEcms和帝国cms哪个比较好,我之前用2个都做过站的,所以能够说出它们大体的区别. 声明:我在此说明的是我一贯用的两种建站体统的感受,没有诋毁或者提升哪个系统!两个系统都 ...