In a binary tree, how do you determine which path is the longest if you have two paths of the same length that are longer than the rest of the other paths? Essentially, no single path is the longest...do you just pick one of the paths or is it first or last encountered path or something?
Asked
Active
Viewed 53 times
0

Dominique Fortin
- 2,212
- 15
- 20

user2499298
- 177
- 2
- 5
- 17
-
This depends very much of why you want the longest path; in many cases (e.g. balancing AVL trees), you need the length of the longest path, not the path itself, so it doesn't matter. If you need the longest path for some other reason .. well then it depends on that reason, there's no generic answer. – Guntram Blohm Mar 01 '14 at 21:30
-
It's for an assignment - the professor wants us to find the longest path in a binary tree after being given the root of the tree. I was able to accomplish this recursively but then I started wondering what I would do if two paths were of equally long length. I reckon I should just ask him then. – user2499298 Mar 01 '14 at 21:33
-
It may very well be a means for the professor to decide which of his students did the homework on their own (the ones that walk up to him and ask that question), and which didnt (those that don't ask). – Guntram Blohm Mar 01 '14 at 21:36
1 Answers
0
If you only need the longest path of a tree and you find two of the equal length, then they are both considered the longest path of that tree and the length of the longest path will equal the length of either one of them.

ucsunil
- 7,378
- 1
- 27
- 32