3

PROBLEM OF THE DAY : 07/02/2024 | Min distance between two given nodes of a Bina...

 7 months ago
source link: https://www.geeksforgeeks.org/videos/problem-of-the-day-07022024-min-distance-between-two-given-nodes-of-a-binary-tree/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Min distance between two given nodes of a Binary Tree

February 08, 2024 |380 Views
PROBLEM OF THE DAY : 07/02/2024 | Min distance between two given nodes of a Binary Tree
Problem of the Day, Data Structure and Algorithm, binary-tree
 Save  Share  1 Like
Description
Discussion

Welcome to the daily solving of our PROBLEM OF THE DAY with Yash Dwivedi. We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Binary Tree but also build up problem-solving skills.

In this problem, we are given a a binary tree with n nodes and two node values, a and b, your task is to find the minimum distance between them. The given two nodes are guaranteed to be in the binary tree and all node values are unique.

Example :

Input:
       1
     /  \
    2    3
a = 2, b = 3
Output: 
2

Explanation: 
We need the distance between 2 and 3. Being at node 2, we need to take two steps ahead to reach node 3. The path followed will be: 2 -> 1 -> 3. Hence, the result is 2. 

Give the problem a try before going through the video. All the best!!!
Problem Link: https://www.geeksforgeeks.org/problems/min-distance-between-two-given-nodes-of-a-binary-tree/1
Solution IDE Link: https://ide.geeksforgeeks.org/online-cpp14-compiler/a9d1cd39-1fac-46c4-ac98-13268250d61f

Read More

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK