Posts

This is a Graph Problem - Not a Tree Problem! Part III

Image
Well it can be seen as a tree problem too, but it becomes easier if you use graph notions. First building the graph from the edges, and then performing a BFS to find the distance from {x,y,z} to each node. Ran into TLE quite a bit hence had to limit the use of Hashtable (still using it, but less) in order to make the cut (barely). Code is down below, cheers, ACC. Pythagorean Distance Nodes in a Tree - LeetCode You are given an integer  n  and an undirected tree with  n  nodes numbered from 0 to  n - 1 . The tree is represented by a 2D array  edges  of length  n - 1 , where  edges[i] = [u i , v i ]  indicates an undirected edge between  u i  and  v i . You are also given three  distinct  target nodes  x ,  y , and  z . For any node  u  in the tree: Let  dx  be the distance from  u  to node  x Let  dy  be the distance from  u  to node  y Let...