ISRO Computer Science (CSE) 2023 - Question 3

All ISRO Computer Science (CSE) papers | ISRO Computer Science (CSE) 2023

Topic: Data Structures

Time complexity to find the diameter of a binary tree having n nodes is

  • A. $O(n^{2})$
  • B. $O(n)$
  • C. $O(1)$
  • D. $O(\log n)$

Answer: B. $O(n)$

Explanation: The diameter of a binary tree can be found in $O(n)$ time by doing a post-order traversal where each node calculates its height and updates the maximum diameter found so far.

| | All questions of ISRO Computer Science (CSE) 2023