Consider this tree:
1
/ \
2 3
/ \
4 5
/
6
At node : left , right . Path through . Return depth .
At node : left , right . Path . Return depth .
At node : left (from ), right . Path . Return depth .
At node : left (from ), right (from ). Path . Return depth .
At node : left (from ), right (from ). Path . Maximum diameter is .
You visit each node once. time, space for the recursion stack where is the tree height.