algorithm - Print the Longest leaf-to-leaf path in a binary tree along with its length -
i solving problem in have find longest leaf-to-leaf path in binary tree along length.
for example, if binary tree follows:
/\ b c / / \ d e f / \ \ g h p \ k
the longest leaf-to-leaf path k-h-d-b-a-c-f-p of length 8.
i calculating length recursively finding length of left , right sub-tree , return height_left + height_right + 1
. concept correct?.
also how should print longest leaf-to-leaf path? want idea proceed.
it seems me algorithm close finding diameter of binary tree. diameter of tree number of nodes on longest path between 2 leaves in tree.
i think can here implementation: http://www.geeksforgeeks.org/diameter-of-a-binary-tree/ , adapt or optimize it's time complexity if want. think o(n)
enough.
Comments
Post a Comment