# Tag Archives: LCA

## Shortest distance between two nodes in BSTOctober 3, 2017

Given a Binary Search Tree and two keys in it. Find the distance between two nodes with given two keys. It may be assumed that… Read More »

## Print common nodes on path from root (or common ancestors)July 4, 2017

Given a binary tree and two nodes, the task is to Print all the nodes that are common for 2 given nodes in a binary… Read More »

## Print the path common to the two paths from the root to the two given nodesJune 19, 2017

Given a binary tree with distinct nodes(no two nodes have the same have data values). The problem is to print the path common to the… Read More »

## Sqrt (or Square Root) Decomposition | Set 2 (LCA of Tree in O(sqrt(height)) time)February 7, 2017

Prerequisite : Introduction and DFS The task is to find LCA of two given nodes in a tree (not necessarily a Binary Tree). In previous… Read More »

## Maximum element between two nodes of BSTJanuary 22, 2017

Given an array of N elements and two integers A, B which belongs to the given array. Create a Binary Search Tree by inserting element… Read More »

## LCA for general or n-ary trees (Sparse Matrix DP approach < O(nlogn), O(logn)>)January 19, 2017

In previous posts, we have discussed how to calculate the Lowest Common Ancestor (LCA) for a binary tree and a binary search tree (this, this… Read More »

## Longest Common Extension / LCE | Set 3 (Segment Tree Method)July 24, 2016

Prerequisites : LCE(Set 1), LCE(Set 2), Suffix Array (n Log Log n), Kasai’s algorithm and Segment Tree The Longest Common Extension (LCE) problem considers a… Read More »

## Longest Common Extension / LCE | Set 1 (Introduction and Naive Method)July 21, 2016

The Longest Common Extension (LCE) problem considers a string s and computes, for each pair (L , R), the longest sub string of s that… Read More »

## Tarjan’s off-line lowest common ancestors algorithmMay 18, 2016

Prerequisite : LCA basics, Disjoint Set Union by Rank and Path Compression We are given a tree(can be extended to a DAG) and we have… Read More »

## Lowest Common Ancestor in a Binary Tree | Set 2 (Using Parent Pointer)March 7, 2016

Given values of two nodes in a Binary Tree, find the Lowest Common Ancestor (LCA). It may be assumed that both nodes exist in the… Read More »

## Heavy Light Decomposition | Set 2 (Implementation)January 17, 2016

We strongly recommend to refer below post as a prerequisite of this. Heavy Light Decomposition | Set 1 (Introduction) In the above post, we discussed… Read More »

## Heavy Light Decomposition | Set 1 (Introduction)January 10, 2016

Heavy Light decomposition (HLD) is one of the most used techniques in competitive programming. Example Problem: Let us understand Heavy-light decomposition (HLD) with the help… Read More »

## Find LCA in Binary Tree using RMQDecember 15, 2015

The article describes an approach to solving the problem of finding the LCA of two nodes in a tree by reducing it to a RMQ… Read More »

## Find distance between two nodes of a Binary TreeFebruary 23, 2014

Find the distance between two keys in a binary tree, no parent pointers are given.

## Lowest Common Ancestor in a Binary Tree | Set 1February 22, 2014

Given a binary tree (not a binary search tree) and two values say n1 and n2, write a program to find the least common ancestor.