Time complexity to find the diameter of a binary tree having n nodes is
2023
Time complexity to find the diameter of a binary tree having n nodes is
- A.
O(n2)
- B.
O(n)
- C.
O(1)
- D.
O(logn)
Attempted by 357 students.
Show answer & explanation
Correct answer: B
The optimal algorithm to find the diameter of a binary tree traverses each node exactly once using post-order traversal. It calculates subtree heights while updating the maximum diameter found, resulting in a linear time complexity of O(n).