Question
Consider a Binary Search Tree (BST) where every node
stores a key and two child pointers. What is the time complexity of finding the Lowest Common Ancestor (LCA) of two nodes in a balanced BST with n nodes?ÂSolution
The time complexity of finding the LCA in a balanced BST is O(log n). A Binary Search Tree (BST) ensures that, for every node, all values in its left subtree are smaller, and all values in its right subtree are larger. To find the LCA of two nodes, you can perform a binary search, comparing the node values with the LCA candidates and traversing left or right subtrees based on comparisons. As the height of a balanced BST is log n, finding the LCA involves a traversal that follows the height of the tree, thus resulting in a time complexity of O(log n). Why Other Options are Incorrect: A) O(n): This is the time complexity of finding the LCA in an unbalanced BST, not a balanced one. C) O(n log n): This time complexity is typically associated with certain sorting algorithms, not BST operations. D) O(1): This is incorrect because finding the LCA requires traversal in a balanced BST. E) O(n² ): This time complexity is associated with certain algorithms in dynamic programming, not for BST operations.
- If the edge of the cube is increased by 5%, then find the percentage increase in the volume of the cube.
The length, breadth and height of a storeroom are 12m, 9m and 10m respectively. If each box occupies 60 m3 of space, then how many boxes can ...
Find the volume of a cylinder with radius 7 cm and height 5 cm. Take π = 22/7.
'O' is a point in the interior of an equilateral triangle. The perpendicular distance from 'O' to the sides are Ö 3 cm, 2Ö 3 cm, 5Ö 3 cm. The perimet...
- The perimeter of a rectangle is equal to that of a square. If the rectangle's length is 22 cm and its breadth is 13 cm, what is the circumference of a semi...
If base of a prism is square of side 8 cm & height is 20 cm . What is the volume of prism?
A hemisphere has a total surface area of 604.8 cm². Calculate the volume of a sphere that has the same radius as the given hemisphere. (Take π = 3.15)...
The length of a rectangle is 7 cm more than the radius of a circle. The perimeter of the rectangle is 60 cm, and the ratio of its length to breadth is 7...
Curved surface area of cylinder is 689.92 cm2. If ratio of radius to height of the cylinder is 16:14 respectively, then find the base area of...
The perimeter of a rectangle of length 2(x + 4) cm and breadth 2(x + 3) cm is double the perimeter of a square of area 484 cm². Find the area of the re...