Find the longest path between any two nodes

I have a binary tree. I need to write a recursive Java method that will give the longest path between two nodes.

For example, the longest path if the next tree is 7 (7-8-5-13-15-18-16-17).

http://img294.imageshack.us/img294/130/treeb.jpg

How to solve this problem?

(Method: public static int longestPath (Node n))

+3
source share
4 answers

Given that this is homework, I would look at β€œDepth Search ” and Width and Width Search .

Preferred for depth

+1
source

, , .

+1

:

-, ?

max .

-, , node .

0

, problem NP-complete, , , .

0

Source: https://habr.com/ru/post/1751870/


All Articles