LC: 1522. Diameter of N Ary Tree - spiralgo/algorithms GitHub Wiki
The Essence:
- Each root node of a tree with 2 or more children has 2 longest paths to its leaves.
- The diameter is the sum of these 2 "arms" of the node + 1.
- A child node should return the longest of these 2 "arms" to its parent for the purposes of calculation in its parent.
Details:
This procedure can be implemented recursively. Firstly, the length of the two longest leaf-paths are calculated. Then they are summed and compared with the global maximum (a child can have the longest diameter since the second "arm" is not returned). The procedure returns the longest leaf-path.