Question
A recursive function calculate_height(node) is designed to find the height of a binary tree (where a single node has height 0). The function is implemented as: def calculate_height(node): if node is None: return -1 # Base case left_height = calculate_height(node.left) right_height = calculate_height(node.right) return 1 + max(left_height, right_height) When tested with a single-node tree (e.g., Node(10)), it returns 0. However, when tested with an empty tree (None), it returns -1. If the desired output for an empty tree is also -1 and for a single node tree is 0, the current implementation is correct. But if the definition of height for a single node tree is 1, what change would be needed in the base case?
More IT Operating System Questions
- The Binary equivalent of the Gray code 1011 will be:
- A disk head is at cylinder 50. Pending requests are 10,20,55,58,90. Under SSTF, which is served next?
- If 1011 is transmitted with alternate-mark-inversion bipolar encoding and the corresponding transmitted voltage levels are {+1,0,-1,+1}. If the received vo...
- Which of the following statements about unguided transmission media is correct?
- In a Real-Time Operating System, what is the most critical requirement for task execution?
- A max-heap is built from the array [3, 9, 2, 1, 4, 5] using standard bottom-up heapify. What is the resulting array?
- Which of the following is TRUE for PROM memories?
- What is a 'semaphore' in Operating Systems?
- What does a star schema consist of?
- A function sum_matrix_elements(matrix, R, C) is designed to sum all elements of a matrix with R rows and C columns. The implementation uses nested loops: ...
Hey! Ask a query
Please enter email id
The email must be a valid email address.
Please enter Mobile Number
Please enter valid Mobile Number
Please enter your Doubt
Think You're Ready for RBI Grade B?
RBI Grade B 2026 Phase 1 Memory Based Paper
- 200 Questions with Detailed Solutions
- Section-wise Coverage (GA, English, Quant & Reasoning)