Total sum except adjacent of a given node in a Binary Tree

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

1. Traverse the tree using pre-order.
2. If current node is adjacent to the key then do not add it to the final sum.
3. If current node is the key then do not add it’s children to the final sum.
4. If key is not present then return sum of all nodes.



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find total sum except a given Node in BT
#include <bits/stdc++.h>
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;
}
  
// sum of all element except those which are adjecent to key Node
void find_sum(Node* root, int key, int& sum, bool incl)
{
    if (root) {
        if (incl) {
            sum += root->data;
  
            if (root->left && root->left->data == key) {
                sum -= root->data;
            }
            else if (root->right && root->right->data == key) {
                sum -= root->data;
            }
        }
  
        incl = root->data == key ? false : true;
        find_sum(root->left, key, sum, incl);
        find_sum(root->right, key, sum, incl);
    }
}
  
// 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;
    int sum = 0;
    find_sum(root, key, sum, true);
    printf("%d ", sum);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find total sum except a given Node in BT
import java.util.*;
class Node
{
    int data;
    Node left, right;
      
    // insertion of Node in Tree
    Node(int key)
    {
        data = key;
        left = right = null;
    }
}
class GFG
{
    static class cal
   {
      int sum = 0;
    }
      
    // sum of all element except those which are adjecent to key Node
    public static void find_sum(Node root, int key, cal r, boolean incl)
    {
        if(root != null)
        {
            if(incl == true)
            {
                r.sum += root.data;
                if((root.left != null) && (root.left.data == key))
                        {
               r.sum -= root.data;
            }
                 else 
                 if((root.right != null) && (root.right.data == key))
                       {
                   r.sum -= root.data;
            }
                  
            }
              
            if(root.data == key)
                incl = false;
                else
                incl = true;
                  
        find_sum(root.left, key, r, incl);
        find_sum(root.right, key, r, incl);
        }
    }
      
// Driver code
public static void main (String[] args) 
{
        Node root = new Node(15);
        root.left = new Node(13);
        root.left.left = new Node(12);
        root.left.left.left = new Node(11);
        root.left.right = new Node(14);
        root.right = new Node(20);
        root.right.left = new Node(18);
        root.right.right = new Node(24);
        root.right.right.right = new Node(25);
        root.right.right.left = new Node(23);
        int key = 20;
         cal obj = new cal();
        find_sum(root, key, obj, true);
        System.out.print(obj.sum);
    }
      
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find total sum 
// except a given Node in BT 
using System;
  
public class Node
{
    public int data;
    public Node left, right;
      
    // insertion of Node in Tree
    public Node(int key)
    {
        data = key;
        left = right = null;
    }
}
public class GFG
{
public class cal
{
    public int sum = 0;
    }
      
    // sum of all element except those
    // which are adjecent to key Node
    public static void find_sum(Node root, 
                    int key, cal r, bool incl)
    {
        if(root != null)
        {
            if(incl == true)
            {
                r.sum += root.data;
                if((root.left != null) && 
                    (root.left.data == key))
                {
                    r.sum -= root.data;
                }
                else
                if((root.right != null) &&
                    (root.right.data == key))
                {
                    r.sum -= root.data;
                }
                  
            }
              
            if(root.data == key)
                incl = false;
            else
                incl = true;
                  
        find_sum(root.left, key, r, incl);
        find_sum(root.right, key, r, incl);
        }
    }
      
// Driver code
public static void Main (String[] args) 
{
        Node root = new Node(15);
        root.left = new Node(13);
        root.left.left = new Node(12);
        root.left.left.left = new Node(11);
        root.left.right = new Node(14);
        root.right = new Node(20);
        root.right.left = new Node(18);
        root.right.right = new Node(24);
        root.right.right.right = new Node(25);
        root.right.right.left = new Node(23);
        int key = 20;
        cal obj = new cal();
        find_sum(root, key, obj, true);
        Console.Write(obj.sum);
    }
}
  
// This code is contributed Rajput-Ji 

chevron_right


Output:

118

Time Complexity : O(n) where n is number of nodes in the BT.



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 : Rajput-Ji