Sunday, 19 December 2021

Most Common Ancestor Between 2 Nodes in a Graph | Algorithms | Data Structure

Find Most Common Ancestor Between 2 Nodes in a Graph. Graph is very simple it has bunch of nodes that may or may not be connected! Representing a graph in code is very easy GRAPH is Important DataStructure for Interview Node is called as Vertices Connections are Edges Graph can scare you if you do not practice them. Once you practice graph it is the simplest Data Structure. You must know DFS, BFS and Traversals. You're given three inputs. all of which are instances of an AncestralTree class that have an ancestor property pointing to their youngest ancestor. The first input is the top ancestor in an ancestral tree (i.e„ the only instance that has no ancestor—its ancestor property points to None null and the other two inputs are descendants in the ancestral tree. Write a function that retunes the youngest common ancestor to the two descendants. Note that a descendant is considered its own ancestor. So in the simple ancestral tree below. the youngest common ancestor to nodes A and B is node A // The youngest common ancestor to nodes A and 8 is node A. Sample Input // The nodes are from the ancestral tree below. node A topAncestor descendantone descendantTwo Sample Output node B node E node I| https://youtu.be/3M3GS2K7rvE| Full Stack Master

No comments:

Post a Comment