Find if there is a triplet in a balanced bst that adds to zeroProblem Statement: Given a balanced (height balanced) binary search tree and the task is to find whether there exists a triplet (3 elements) that sum up to 0 and if present return present else not present Input: Output: {-13, 6, 7} The straightforward approach involves examining every triplet within the Binary Search Tree (BST) and verifying whether the sum equals zero. This method carries a time complexity of O(n^3). An improved strategy entails generating an auxiliary array to store the Inorder traversal of the Binary Search Tree (BST). This array is guaranteed to be sorted, as the Inorder traversal of a BST consistently yields data in sorted order. Better version The provided solution operates in O(n^2) time complexity and utilizes O(Logn) additional space:
Java Implementation: Output: Time Complexity: O(n^2) Space Complexity: O(log(n)) |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India