Suma total excepto adyacente de un Node dado en BST

Dado un BST y un Node clave, encuentre la suma total en BST, excepto aquellos Nodes que son adyacentes al Node clave. 

Ejemplos:

1:-Primero encuentre la suma total de BST 
2:-Busque el Node clave y rastree su Node principal. 
3:-Si el Node clave está presente, reste la suma de su Node adyacente de la suma total 
4:-Si la clave no está presente en BST, devuelva -1.

C++

// C++ program to find total sum except a given Node in BST
#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;
}
 
// 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 adjacent 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 adjacent Node and
            // subtract 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 adjacent 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 adjacent Node and
            // subtract 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

Python3

# Python3 program to find total sum
# except a given Node in BST
class getNode:
     
    def __init__(self, n):
         
        self.data = n
        self.left = None
        self.right = None
 
# Total sum of bst
def sum(root):
     
    if (root == None):
        return 0
 
    return (root.data + sum(root.left) +
                        sum(root.right))
 
# Sum of all element except those
# which are adjacent to key Node
def adjSum(root, key):
     
    parent = root.data
 
    while (root != None):
        if (key < root.data):
            parent = root.data
            root = root.left
        elif (root.data == key):
             
            # Key Node matches
            # if the left Node and right Node of key is
            # not None then add all adjacent Node and
            # subtract from totalSum
            if (root.left != None and
               root.right != None):
                return (parent + root.left.data +
                                 root.right.data)
 
            # If key is leaf
            if (root.left == None and
               root.right == None):
                return parent
 
            # If only left child is None
            if (root.left == None):
                return (parent + root.right.data)
 
            # If only right child is None
            if (root.right == None):
                return (parent + root.left.data)
 
        else:
            parent = root.data
            root = root.right
 
    return 0
 
def findTotalExceptKey(root, key):
     
    return sum(root) - adjSum(root, key)
 
# Driver code
if __name__ == '__main__':
     
    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)
     
    key = 20
     
    print(findTotalExceptKey(root, key))
     
# This code is contributed by bgangwar59

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 adjacent 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 adjacent Node and
            // subtract 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

Javascript

<script>
 
// Javascript program to find total sum
// except a given Node in BST
class Node
{
    constructor(data)
    {
        this.left = null;
        this.right = null;
        this.data = data;
    }
};
 
// Insertion of Node in Tree
function getNode(n)
{
    let root = new Node(n);
    return root;
}
 
// Total sum of bst
function sum(root)
{
    if (root == null)
        return 0;
 
    return root.data + sum(root.left) +
                       sum(root.right);
}
 
// Sum of all element except those
// which are adjacent to key Node
function adjSum(root, key)
{
    let parent = root.data;
 
    while (root != null)
    {
        if (key < root.data)
        {
            parent = root.data;
            root = root.left;
        }
         
        // key Node matches
        else if (root.data == key)
        {
             
            // If the left Node and right Node of key is
            // not null then add all adjacent Node and
            // subtract 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;
}
 
function findTotalExceptKey(root, key)
{
    return sum(root) - adjSum(root, key);
}
 
// Driver code
let 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);
 
let key = 20;
document.write(findTotalExceptKey(root, key));
 
// This code is contributed by divyeshrabadiya07
 
</script>
Producción: 

118

 

Complejidad de tiempo: O(n) + O(h) donde n es el número de Nodes en BST yh es la altura de BST. Podemos escribir la complejidad del tiempo como O(n).
 

Publicación traducida automáticamente

Artículo escrito por Mohd_Saliem y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *