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
- Which network security device typically enforces a policy based on IP, port and protocol and can operate at transport and application layer when deep inspe...
- Which algorithm is used in game playing for decision making?
- A C function print_matrix(int rows, int cols, int matrix[rows][cols]) is designed to print a matrix. #include void print_matrix(int rows, int cols, ...
- Consider the following C code snippet: #include int factorial(int n) { if (n == 0) { return 1; } // ...
- Consider the following C code snippet for calculating base raised to the power of exp: #include double power(double base, int exp) { ...
- Which of the following algorithms is a classic example of the Divide and Conquer paradigm?
- In a language like C++, if a member variable is declared protected, it means:
- Which algorithm divides the input array into two halves, sorts each half, and then merges the sorted halves?
- Consider the following C++ code: class Base { public: void show() { std::cout
- CDATA is used in XML to:
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