Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The diameter of a tree T is the largest of the following quantities:

  1. the diameter of T's left subtree
  2. the diameter of T's right subtree
  3. the longest path between leaves that goes through the root of T (this can be computed from the heights of the subtrees of T)

Source: https://www2.cs.duke.edu/courses/spring00/cps100/assign/trees/diameter.html

However, it doesn't say what is the diameter of tree that doesn't have at least two leaves, like a root-only tree, or 1 -> 2? Is it 0, undefined, infinity or negative infinity?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

A better definition of diameter is simply "the length of the longest path". The root only tree has diameter 0, and the path on two vertices has diameter 1. The rules given are "unnecessarily" complicated, but that's because they also give a way to calculate the diameter. Also, note that, in a graph theoretic sense, if the root is missing all or all but one of its children, it is a leaf itself, and so you can see the third clause as actually covering the problem cases.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...