Total sum except adjacent of a given node in BST

Given a BST and a key Node, find the total sum in BST, except those Node which are adjacent to key Node.
Examples:

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

1:-First find the total sum of BST
2:-Search the key Node and trace its parent Node.
3:-If the key Node is present then, subtract the sum of its adjacent Node from total sum
4:-If key is not present in BST then return -1.

C++

 `// C++ program to find total sum except a given Node in BST ` `#include ` `using` `namespace` `std; ` ` `  `struct` `Node { ` `    ``int` `data; ` `    ``struct` `Node *left, *right; ` `}; ` ` `  `// insertion of Node in Tree ` `Node* getNode(``int` `n) ` `{ ` `    ``struct` `Node* root = ``new` `Node; ` `    ``root->data = n; ` `    ``root->left = NULL; ` `    ``root->right = NULL; ` `    ``return` `root; ` `} ` ` `  `// total sum of bst ` `int` `sum(``struct` `Node* root) ` `{ ` `    ``if` `(root == NULL) ` `        ``return` `0; ` ` `  `    ``return` `root->data + sum(root->left) + sum(root->right); ` `} ` ` `  `// sum of all element except those which are adjecent to key Node ` `int` `adjSum(Node* root, ``int` `key) ` `{ ` ` `  `    ``int` `parent = root->data; ` ` `  `    ``while` `(root != NULL) { ` `        ``if` `(key < root->data) { ` `            ``parent = root->data;  ` `            ``root = root->left; ` `        ``} ` `        ``else` `if` `(root->data == key) ``// key Node matches ` `        ``{ ` `            ``// if the left Node and right Node of key is  ` `            ``// not null then add all adjecent Node and ` `            ``// substract from totalSum ` `            ``if` `(root->left != NULL && root->right != NULL) ` `                ``return` `(parent + root->left->data +  ` `                                 ``root->right->data); ` ` `  `            ``// if key is leaf ` `            ``if` `(root->left == NULL && root->right == NULL) ` `                ``return` `parent; ` ` `  `            ``// If only left child is null ` `            ``if` `(root->left == NULL)  ` `                ``return` `(parent + root->right->data); ` ` `  `            ``// If only right child is NULL ` `            ``if` `(root->right == NULL)  ` `                ``return` `(parent + root->left->data); ` `        ``} ` ` `  `        ``else` `{ ` `            ``parent = root->data; ` `            ``root = root->right; ` `        ``} ` `    ``} ` ` `  `    ``return` `0; ` `} ` ` `  `int` `findTotalExceptKey(Node *root, ``int` `key) ` `{ ` `    ``return` `sum(root) - adjSum(root, key); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``struct` `Node* root = getNode(15); ` `    ``root->left = getNode(13); ` `    ``root->left->left = getNode(12); ` `    ``root->left->left->left = getNode(11); ` `    ``root->left->right = getNode(14); ` `    ``root->right = getNode(20); ` `    ``root->right->left = getNode(18); ` `    ``root->right->right = getNode(24); ` `    ``root->right->right->left = getNode(23); ` `    ``root->right->right->right = getNode(25); ` `    ``int` `key = 20; ` `    ``printf``(``"%d "``, findTotalExceptKey(root, key)); ` `    ``return` `0; ` `} `

Java

 `// Java program to find total sum ` `// except a given Node in BST ` `class` `GFG ` `{ ` `static` `class` `Node ` `{ ` `    ``int` `data; ` `    ``Node left, right; ` `}; ` ` `  `// insertion of Node in Tree ` `static` `Node getNode(``int` `n) ` `{ ` `    ``Node root = ``new` `Node(); ` `    ``root.data = n; ` `    ``root.left = ``null``; ` `    ``root.right = ``null``; ` `    ``return` `root; ` `} ` ` `  `// total sum of bst ` `static` `int` `sum(Node root) ` `{ ` `    ``if` `(root == ``null``) ` `        ``return` `0``; ` ` `  `    ``return` `root.data + sum(root.left) +  ` `                       ``sum(root.right); ` `} ` ` `  `// sum of all element except those ` `// which are adjecent to key Node ` `static` `int` `adjSum(Node root, ``int` `key) ` `{ ` `    ``int` `parent = root.data; ` ` `  `    ``while` `(root != ``null``)  ` `    ``{ ` `        ``if` `(key < root.data)  ` `        ``{ ` `            ``parent = root.data;  ` `            ``root = root.left; ` `        ``} ` `        ``else` `if` `(root.data == key) ``// key Node matches ` `        ``{ ` `            ``// if the left Node and right Node of key is  ` `            ``// not null then add all adjecent Node and ` `            ``// substract from totalSum ` `            ``if` `(root.left != ``null` `&& root.right != ``null``) ` `                ``return` `(parent + root.left.data +  ` `                                ``root.right.data); ` ` `  `            ``// if key is leaf ` `            ``if` `(root.left == ``null` `&& root.right == ``null``) ` `                ``return` `parent; ` ` `  `            ``// If only left child is null ` `            ``if` `(root.left == ``null``)  ` `                ``return` `(parent + root.right.data); ` ` `  `            ``// If only right child is null ` `            ``if` `(root.right == ``null``)  ` `                ``return` `(parent + root.left.data); ` `        ``} ` `        ``else`  `        ``{ ` `            ``parent = root.data; ` `            ``root = root.right; ` `        ``} ` `    ``} ` `    ``return` `0``; ` `} ` ` `  `static` `int` `findTotalExceptKey(Node root, ``int` `key) ` `{ ` `    ``return` `sum(root) - adjSum(root, key); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``Node root = getNode(``15``); ` `    ``root.left = getNode(``13``); ` `    ``root.left.left = getNode(``12``); ` `    ``root.left.left.left = getNode(``11``); ` `    ``root.left.right = getNode(``14``); ` `    ``root.right = getNode(``20``); ` `    ``root.right.left = getNode(``18``); ` `    ``root.right.right = getNode(``24``); ` `    ``root.right.right.left = getNode(``23``); ` `    ``root.right.right.right = getNode(``25``); ` `    ``int` `key = ``20``; ` `    ``System.out.printf(``"%d "``,  ` `               ``findTotalExceptKey(root, key)); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

C#

 `// C# program to find total sum ` `// except a given Node in BST ` `using` `System; ` ` `  `class` `GFG ` `{ ` `class` `Node ` `{ ` `    ``public` `int` `data; ` `    ``public` `Node left, right; ` `}; ` ` `  `// insertion of Node in Tree ` `static` `Node getNode(``int` `n) ` `{ ` `    ``Node root = ``new` `Node(); ` `    ``root.data = n; ` `    ``root.left = ``null``; ` `    ``root.right = ``null``; ` `    ``return` `root; ` `} ` ` `  `// total sum of bst ` `static` `int` `sum(Node root) ` `{ ` `    ``if` `(root == ``null``) ` `        ``return` `0; ` ` `  `    ``return` `root.data + sum(root.left) +  ` `                       ``sum(root.right); ` `} ` ` `  `// sum of all element except those ` `// which are adjecent to key Node ` `static` `int` `adjSum(Node root, ``int` `key) ` `{ ` `    ``int` `parent = root.data; ` ` `  `    ``while` `(root != ``null``)  ` `    ``{ ` `        ``if` `(key < root.data)  ` `        ``{ ` `            ``parent = root.data;  ` `            ``root = root.left; ` `        ``} ` `        ``else` `if` `(root.data == key) ``// key Node matches ` `        ``{ ` `            ``// if the left Node and right Node of key is  ` `            ``// not null then add all adjecent Node and ` `            ``// substract from totalSum ` `            ``if` `(root.left != ``null` `&& root.right != ``null``) ` `                ``return` `(parent + root.left.data +  ` `                                 ``root.right.data); ` ` `  `            ``// if key is leaf ` `            ``if` `(root.left == ``null` `&& root.right == ``null``) ` `                ``return` `parent; ` ` `  `            ``// If only left child is null ` `            ``if` `(root.left == ``null``)  ` `                ``return` `(parent + root.right.data); ` ` `  `            ``// If only right child is null ` `            ``if` `(root.right == ``null``)  ` `                ``return` `(parent + root.left.data); ` `        ``} ` `        ``else` `        ``{ ` `            ``parent = root.data; ` `            ``root = root.right; ` `        ``} ` `    ``} ` `    ``return` `0; ` `} ` ` `  `static` `int` `findTotalExceptKey(Node root, ``int` `key) ` `{ ` `    ``return` `sum(root) - adjSum(root, key); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``Node root = getNode(15); ` `    ``root.left = getNode(13); ` `    ``root.left.left = getNode(12); ` `    ``root.left.left.left = getNode(11); ` `    ``root.left.right = getNode(14); ` `    ``root.right = getNode(20); ` `    ``root.right.left = getNode(18); ` `    ``root.right.right = getNode(24); ` `    ``root.right.right.left = getNode(23); ` `    ``root.right.right.right = getNode(25); ` `    ``int` `key = 20; ` `    ``Console.Write(``"{0} "``,  ` `            ``findTotalExceptKey(root, key)); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```118
```

Time Complexity : O(n) + O(h) where n is number of nodes in BST and h is height of BST. We can write time complexity as O(n).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : 29AjayKumar, princiraj1992

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.