[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 ...
随机推荐
- Spring Boot 基础配置
之前简单接触了一些Spring Boot ,并且写了一个简单的 Demo .本文就来简单学习一下 Spring Boot 的基础配置. 一.Spring Boot 项目入口 上文中有写到,Spring ...
- Java重写、重载与覆盖
Java继承.重载与重写 一.继承(单继承) 1.利用extends关键字一个方法继承另一个方法,而且只能直接继承一个类. 2.当Sub类和Base类在同一个包时,Sub类继承Base类中的publi ...
- Linux下gcc与g++用法以及编写makefile
1. gcc与g++编译流程: 1) 编译流程: 2) 预处理:生成.i的预处理文件. Ø 只激活预处理,这个不生成文件,需要把它重定向一个输出文件. ...
- zoj 3469 区间dp **
题意:有一家快餐店送外卖,现在同时有n个家庭打进电话订购,送货员得以V-1的速度一家一家的运送,但是每一个家庭都有一个不开心的值,每分钟都会增加一倍,值达到一定程度,该家庭将不会再订购外卖了,现在为了 ...
- python开发_thread_线程_搜索本地文件
在之前的blog中,曾经写到过关于搜索本地文件的技术文章 如: java开发_快速搜索本地文件_小应用程序 python开发_搜索本地文件信息写入文件 下面说说python中关于线程来搜索本地文件 利 ...
- hdoj 4445 Crazy Tank 物理题/枚举角度1
Crazy TankTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 分布式文件系统 ~MogileFS~
一.分布式文件系统 分布式文件系统(Distributed File System)是指文件系统管理的物理存储资源不一定直接连接在本地节点上,而是通过计算机网络与节点相连,也就是集群文件系统,可以支持 ...
- Ubuntu16.04中安装stlink驱动
系统环境: Vmware12, Ubuntu16.04 Stlink version:v1.4.0 一.安装依赖包: sudo apt-get install libusb-1.0 sudo apt- ...
- BTSync FREE vs BTSync PRO
Although both BitTorrent Sync 2.0 FREE and PRO ensure high file transfer speed and ultimate security ...
- %( $# > 1 %? if (tid() in trace) %) 是什么意思
http://blog.csdn.net/sunnybeike/article/details/7769663 http://blog.163.com/digoal@126/blog/static/1 ...