http://www.geeksforgeeks.org/find-if-there-is-a-triplet-in-bst-that-adds-to-0/
Given a Balanced Binary Search Tree (BST), write a function isTripletPresent() that returns true if there is a triplet in given BST with sum equals to 0, otherwise returns false. Expected time complexity is O(n^2) and only O(Logn) extra space can be used. You can modify given Binary Search Tree. Note that height of a Balanced BST is always O(Logn)
Key Point: if we can't have extra space, consider changing the structure: convert tree to double linked list.
1) Convert given BST to Doubly Linked List (DLL)
2) Now iterate through every node of DLL and if the key of node is negative, then find a pair in DLL with sum equal to key of current node multiplied by -1
Time Complexity: Time taken to convert BST to DLL is O(n) and time taken to find triplet in DLL is O(n^2).
http://www.geeksforgeeks.org/find-a-triplet-that-sum-to-a-given-value/
1) Sort the input array.
2) Fix the first element as A[i] where i is from 0 to array size – 2. After fixing the first element of triplet, find the other two elements using method 1 of this post
http://hrishikeshmishra.com/find-if-there-is-a-triplet-in-a-balanced-bst-that-adds-to-zero/
Given a Balanced Binary Search Tree (BST), write a function isTripletPresent() that returns true if there is a triplet in given BST with sum equals to 0, otherwise returns false. Expected time complexity is O(n^2) and only O(Logn) extra space can be used. You can modify given Binary Search Tree. Note that height of a Balanced BST is always O(Logn)
Key Point: if we can't have extra space, consider changing the structure: convert tree to double linked list.
1) Convert given BST to Doubly Linked List (DLL)
2) Now iterate through every node of DLL and if the key of node is negative, then find a pair in DLL with sum equal to key of current node multiplied by -1
bool
isTripletPresent(node *root)
{
// Check if the given BST is empty
if
(root == NULL)
return
false
;
// Convert given BST to doubly linked list. head and tail store the
// pointers to first and last nodes in DLLL
node* head = NULL;
node* tail = NULL;
convertBSTtoDLL(root, &head, &tail);
// Now iterate through every node and find if there is a pair with sum
// equal to -1 * heaf->key where head is current node
while
((head->right != tail) && (head->key < 0))
{
// If there is a pair with sum equal to -1*head->key, then return
// true else move forward
if
(isPresentInDLL(head->right, tail, -1*head->key))
return
true
;
else
head = head->right;
}
// If we reach here, then there was no 0 sum triplet
return
false
;
}
void
convertBSTtoDLL(node* root, node** head, node** tail)
{
// Base case
if
(root == NULL)
return
;
// First convert the left subtree
if
(root->left)
convertBSTtoDLL(root->left, head, tail);
// Then change left of current root as last node of left subtree
root->left = *tail;
// If tail is not NULL, then set right of tail as root, else current
// node is head
if
(*tail)
(*tail)->right = root;
else
*head = root;
// Update tail
*tail = root;
// Finally, convert right subtree
if
(root->right)
convertBSTtoDLL(root->right, head, tail);
}
// This function returns true if there is pair in DLL with sum equal
// to given sum. The algorithm is similar to hasArrayTwoCandidates()
// in method 1 of http://tinyurl.com/dy6palr
bool
isPresentInDLL(node* head, node* tail,
int
sum)
{
while
(head != tail)
{
int
curr = head->key + tail->key;
if
(curr == sum)
return
true
;
else
if
(curr > sum)
tail = tail->left;
else
head = head->right;
}
return
false
;
}
Auxiliary Space: The auxiliary space is needed only for function call stack in recursive function convertBSTtoDLL(). Since given tree is balanced (height is O(Logn)), the number of functions in call stack will never be more than O(Logn).
Find a triplet that sum to a given valuehttp://www.geeksforgeeks.org/find-a-triplet-that-sum-to-a-given-value/
1) Sort the input array.
2) Fix the first element as A[i] where i is from 0 to array size – 2. After fixing the first element of triplet, find the other two elements using method 1 of this post
bool
find3Numbers(
int
A[],
int
arr_size,
int
sum)
{
int
l, r;
/* Sort the elements */
quickSort(A, 0, arr_size-1);
/* Now fix the first element one by one and find the
other two elements */
for
(
int
i = 0; i < arr_size - 2; i++)
{
// To find the other two elements, start two index variables
// from two corners of the array and move them toward each
// other
l = i + 1;
// index of the first element in the remaining elements
r = arr_size-1;
// index of the last element
while
(l < r)
{
if
( A[i] + A[l] + A[r] == sum)
{
printf
(
"Triplet is %d, %d, %d"
, A[i], A[l], A[r]);
return
true
;
}
else
if
(A[i] + A[l] + A[r] < sum)
l++;
else
// A[i] + A[l] + A[r] > sum
r--;
}
}
// If we reach here, then no triplet was found
return
false
;
}
private static class Container {
private BinaryTreeNode<Integer> dllHead;
private BinaryTreeNode<Integer> dllTail;
}
public static boolean isTripletPresent(BinaryTreeNode<Integer> root) {
Container container = new Container();
createDLL(root, container);
BinaryTreeNode<Integer> temp = container.dllHead;
/** Traverse DLL **/
while (!Objects.isNull(temp)) {
/** Check current node data is negative **/
if (isNegativeNumber(temp.getData())) {
/** Check is there any two node, whose sum equal to current node data **/
if (checkSum(container.dllHead, container.dllTail, makePositive(temp.getData()))) {
return true;
}
}
}
return false;
}
private static int makePositive(int number) {
return number * -1;
}
private static boolean checkSum(BinaryTreeNode<Integer> head, BinaryTreeNode<Integer> tail, int sum) {
/** Iterate until head == tail **/
while (head != tail) {
/** Calculate sum of two nodes **/
int localSum = head.getData() + tail.getData();
/** If both sum equal to each other return true **/
if (localSum == sum) {
return true;
} else if (localSum < sum) {
/** If localSum lesser then move head right to get higher value node **/
head = head.getRight();
} else {
/** If local sum larger then move tail left to get lesser value node **/
tail = tail.getLeft();
}
}
return false;
}
private static boolean isNegativeNumber(int number) {
return number < 0;
}
private static void createDLL(BinaryTreeNode<Integer> root, Container container) {
/** When root is null **/
if (Objects.isNull(root)) {
return;
}
/** Process InOrder fashion **/
/** Go extreme left **/
if (!Objects.isNull(root.getLeft())) {
createDLL(root.getLeft(), container);
}
/** Previous node right pointer to current node **/
if (!Objects.isNull(container.dllTail)) {
container.dllTail.setRight(root);
} else {
/** Will execute only time to update dll head pointer **/
container.dllHead = root;
}
root.setLeft(container.dllTail);
container.dllTail = root;
if (!Objects.isNull(root.getRight())) {
createDLL(root.getRight(), container);
}
}