# Lowest Common Ancestor of a Binary Tree — Day 10(Python)

We will be looking at a Tree Problem today. Before jumping into the problem, let us understand a few features of the binary tree. A binary tree is a data structure where each node can have at the most two children. The topmost part of a tree is called the root node, and the ...

We will be looking at a Tree Problem today. Before jumping into the problem, let us understand a few features of the binary tree.

A binary tree is a data structure where each node can have at the most two children. The topmost part of a tree is called the root node, and the ends are called the leaf. A tree of any form cannot have a cycle in it. An example of a binary tree is as follows.

Example of a binary tree.

236. 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 to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]

Example 1:

``````Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.``````

Example 2:

``````Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.``````

Note:

• All of the nodes’ values will be unique.
• p and q are different and both values will exist in the binary tree.

Solution:

1. Maintain a dictionary that provides information about the child node and their parent node.

a. Use a stack that holds nodes to be visited.

b. Use a dictionary that holds information about the child node and their parent node. Initialize it with key as root and value as none, which implies the root does not have any parent node.

c. Run the loop until elements are present in the stack.

i. Pop node from the stack.

ii. Check if the node has any left child. If yes, push the left child of the node to stack, and in the dictionary mention key as left child and value as the current node. Perform the same operation if we have the right child for the current node.

d. Once the tree is traversed completely, create a set that will hold ancestors from node1 to the root node.

e. Traverse from node2 to root node, and check if any of the nodes are present in the above set. If we find a common node, return the node value.

The algorithm will look as below.

## 13 Free/Low-Cost Sites to Supercharge Your Programming Self-Education

Although we still talk about programming as a standalone career, the dominance of technology in our lives makes it clear that coding is much more than a career path. In my opinion, computer science is more than a college major or a high-paid job; it’s a skill, essential for thriving in a modern-day economy. Whether you work in healthcare, marketing, business, or other fields, you will see more coding and have to deal with a growing number of technologies throughout your entire life.

## How to Structure Your Programming Code

I remember my first fumble with basic on my ZX Spectrum computer back in the 1980s, ploughing through pages of basic commands and example code without any real idea of how I could write programs myself

## What Is Code Golfing And Biggest Such Tournaments

Code golf is a type of recreational computer programming competition in which participants strive to achieve the shortest possible source code that implements a certain algorithm. Playing code golf is known as "golf scripting". Code golf challenges and tournaments may also be named with the ... Some code golf questions, such as those posed on general programming ...

## Coding 101: Programming Language Building Blocks

This article will introduce the concepts and topics common to all programming languages, that beginners and experts must know!

## Static Code Analysis: What It Is? How to Use It?

Static code analysis is a method of debugging by examining source code before a program is run. It's done by analyzing a set of code against a set (or multiple sets) of coding rules. Static code analysis and static analysis are often used interchangeably, along with source code analysis.