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
- In a recursive subset_sum function, backtrack(index, current_subset, current_sum), to explore the option of *including* the current element arr[index], whi...
- Locality of Reference are of the following two types:
- In the Banker's Algorithm used for deadlock avoidance, a system is said to be in a 'safe state' when:
- Why is a unique salt added before hashing each stored password?
- A system uses paging with page size 1 KB. Logical address 2500 maps to page 2 (2500 ÷ 1024 = 2 remainder 452), and the page table entry for page 2 gives fr...
- Which heuristic search strategy uses both cost so far and estimated cost to goal?
- Which constraint ensures that a column cannot store NULL values and also guarantees that all values in the column are unique?
- In C the array index starts from :
- 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...
- What is perplexity in language models?
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)