Lowest Common Ancestor of a Binary Tree17 Mar 2025 | 4 min read What does the binary tree's lowest common ancestor represent?The lowest node in the tree that contains both n1 and n2 as descendants is the lowest common ancestor (LCA), and n1 and n2 are the nodes for which we are looking for the LCA. As a result, the common ancestor of nodes n1 and n2 that is placed the furthest from the root is the LCA of a binary tree containing nodes n1 and n2. LCA (Lowest Common Ancestor) application:The gap between pairs of nodes in a tree can be computed as the distance from the root to n1 plus the distance from the root to n2 minus twice the distance from the root to their lowest common ancestor. Example:![]() Approach-
Program in C++:Recursive Approach Using single Traversal Time complexity: O(n) Space complexity: O(n) Function to print LCA: Input: Output: 3 Iterative ApproachUsing Double Traversal and finding path for node p and q respectively than comparing their path to find LCA. Complexity Time complexity: O(n)+O(n) Space complexity: O(n)+O(n) Function to print LCA: Input: Output: 3 Function to print LCA in Python:Input: Output: 3 Function to print LCA in Java:Input: Output: 3 Next TopicTop View of a Binary Tree |
Different Types of Stack Operations
Introduction: In computer science, a stack is a fundamental data structure that follows the Last-In-First-Out (LIFO) principle.It is an abstract data type containing a number of pieces that can handle push and pop as its two primary operations.The push action adds an element to the top of...
13 min read
Generating the Maximum Number from Two Arrays
Problem Statement We are provided with two integer arrays, nums1, and nums2, each representing the digits of two numbers. Additionally, an integer k is given. Our task is to construct the maximum number of length k (where k is less than or equal to the sum of...
11 min read
Weighted Prefix Search
In the fields of information retrieval and natural language processing, weighted prefix search is a potent idea that is essential to many different applications, from recommendation engines to search engines. We shall examine the importance, uses, and underlying techniques of weighted prefix search in this article's...
6 min read
Merge two binary Max Heaps
. Heaps are tree-based data structures that are commonly used to implement priority queues. They allow efficient access to the maximum or minimum element. Sometimes, we must combine two heaps into a single merged heap containing all elements from both. This allows for implementation priority queues that...
7 min read
Design a data structure that supports insert, delete, search, and getRandom in constant time
Designing a data structure that allows for constant-time insertion, deletion, searching, and random access is an interesting issue in computer science. Obtaining consistent time complexity for these activities sometimes necessitates a trade-off between various characteristics of data storage and access. This article delves into the core...
5 min read
Tarjan's Algorithm to Find Strongly Connected Components
Introduction In this article, we'll delve into Tarjan's Algorithm, figure out its inward operations, and execute it in C. Strongly Connected Components are fundamental designs in graph theory, addressing subsets of vertices where every vertex is reachable from every vertex inside the subset. Recognizing Strongly Connected Components in...
5 min read
Priority Queue using Linked list
A priority queue is a type of queue in which each element in a queue is associated with some priority, and they are served based on their priorities. If the elements have the same priority, they are served based on their order in a queue. Mainly, the...
3 min read
Proof that vertex cover is NP-complete
Introduction An essential area of math called graph theory which is numerical systems that address pairwise relationships between objects. In graph theory, there are numerous problems one of which is the Vertex Cover problem. In computer science & combinatorial optimization, Vertex Cover is a classic issue with...
4 min read
Unrolled Linked List
A linear data structure called an unrolled linked list is a variant on the linked list. Unrolled linked lists store a full array at each node instead of simply one element at each node. Unrolled linked lists combine the advantages of an array (low memory overhead)...
14 min read
Binary Tree Traversal in DS
Binary Tree Traversal in Data Structure The tree can be defined as a non-linear data structure that stores data in the form of nodes, and nodes are connected to each other with the help of edges. Among all the nodes, there is one main node called the...
24 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India
