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

  1. A.

    O(n2)

  2. B.

    O(n)

  3. C.

    O(1)

  4. 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).

Explore the full course: Isro