This is a typical problem of recursion.
The diameter of a tree is the number of nodes on the longest path between two leaves in the tree. So you can compute it as the max among the height for the left subtree, the height of the right subtree and the longest path between two leaves passing through the root.
http://tech-queries.blogspot.com/2010/09/diameter-of-tree-in-on.html
ReplyDelete