[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 ...
随机推荐
- [HNOI2008]玩具装箱
OJ题号: BZOJ1010 思路: 斜率优化动态规划. 由题意得状态转移方程为$f_i=\displaystyle{\min_{j=0}^{i-1}}\{f_j+\left(i-j-1+\displ ...
- FLASK开发简易blog的学习笔记
首先,学会了如何创建一个新的包.就是在文件夹下创建__init__.py文件,就是在其他地方导入这个包了.
- python开发_xml.dom_解析XML文档_完整版_博主推荐
在阅读之前,你需要了解一些xml.dom的一些理论知识,在这里你可以对xml.dom有一定的了解,如果你阅读完之后. 下面是我做的demo 运行效果: 解析的XML文件位置:c:\\test\\hon ...
- Codeforces Round #298 (Div. 2) B. Covered Path 物理题/暴力枚举
B. Covered Path Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/probl ...
- hdoj 4272 LianLianKan 数据太水
LianLianKan Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- python实现并查集
并查集是这样的数据结构:有一大堆的数据,把一些元素放在一个集合当中,另外一些元素放在另一个一个集合当中. 对于它的操作有:查看两个元素是否在一个集合当中.合并两个元素. 合并的时候采取的策略是这样的: ...
- .net正则提取手机号码,并替换带有手机号码的a标签
//用正则查找字符串中的手机号码,最后替换成带a标签的可打电话的字符串 var str = "收件人:江苏省苏州市工业园区屌丝大叔收,电话:18688888888"; var re ...
- ThinkPHP中RBAC权限管理的简单应用
RBAC英文全称(Role-Based Access Controller)即基于角色的权限访问控制,简单来讲,一个用户可以拥有若干角色,每一个角色拥有若干权限.这样,就构造成“用户-角色-权限”的授 ...
- 使用 soapUI 测试 REST 服务
REST 服务介绍 REST(Representational State Transfer)是 Roy Fielding 博士在 2000 年提出的一种新的软件架构风格,它以资源(resource) ...
- 给Android组件添加事件一个很好用的方法
在这里想和大家分享一下很好用的添加事件方法,特别是在处理ListView里的Item事件的时候,很方便. 首先,在XML里布局的时候,添加这样一个属性: android:onClick="C ...