📜  在 BST 中找到给定总和的对

📅  最后修改于: 2021-10-27 07:28:19             🧑  作者: Mango

给定一个 BST 和一个总和,找出是否存在一个给定总和的对。

例子:

Input : sum = 28
        Root of below tree

Output : Pair is found (16, 12)

推荐:请先在“PRACTICE”上解决,然后再继续解决

我们在下面的帖子中讨论了找到具有给定总和的对的不同方法。 在平衡 BST 中找到具有给定总和的对
在这篇文章中,讨论了基于散列的解决方案。我们通过中序遍历二叉搜索树并将节点的值插入到一个集合中。还要检查任何节点,给定总和和节点值之间的差异,如果找到,则对存在,否则不存在。

C++
// CPP program to find a pair with
// given sum using hashing
#include 
using namespace std;
 
struct Node {
    int data;
    struct Node *left, *right;
};
 
Node* NewNode(int data)
{
    Node* temp = (Node*)malloc(sizeof(Node));
    temp->data = data;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}
 
Node* insert(Node* root, int key)
{
    if (root == NULL)
        return NewNode(key);
    if (key < root->data)
        root->left = insert(root->left, key);
    else
        root->right = insert(root->right, key);
    return root;
}
 
bool findpairUtil(Node* root, int sum,
                  unordered_set& set)
{
    if (root == NULL)
        return false;
 
    if (findpairUtil(root->left, sum, set))
        return true;
 
    if (set.find(sum - root->data) != set.end()) {
        cout << "Pair is found (" << sum - root->data
             << ", " << root->data << ")" << endl;
        return true;
    }
    else
        set.insert(root->data);
 
    return findpairUtil(root->right, sum, set);
}
 
void findPair(Node* root, int sum)
{
    unordered_set set;
    if (!findpairUtil(root, sum, set))
        cout << "Pairs do not exit" << endl;
}
 
// Driver code
int main()
{
    Node* root = NULL;
    root = insert(root, 15);
    root = insert(root, 10);
    root = insert(root, 20);
    root = insert(root, 8);
    root = insert(root, 12);
    root = insert(root, 16);
    root = insert(root, 25);
    root = insert(root, 10);
 
    int sum = 33;
    findPair(root, sum);
 
    return 0;
}


Java
// JAVA program to find a pair with
// given sum using hashing
 
import java.util.*;
 
class GFG {
 
    static class Node {
        int data;
        Node left, right;
    };
 
    static Node NewNode(int data)
    {
        Node temp = new Node();
        temp.data = data;
        temp.left = null;
        temp.right = null;
        return temp;
    }
 
    static Node insert(Node root, int key)
    {
        if (root == null)
            return NewNode(key);
        if (key < root.data)
            root.left = insert(root.left, key);
        else
            root.right = insert(root.right, key);
        return root;
    }
 
    static boolean findpairUtil(Node root, int sum,
                                HashSet set)
    {
        if (root == null)
            return false;
 
        if (findpairUtil(root.left, sum, set))
            return true;
 
        if (set.contains(sum - root.data)) {
            System.out.println("Pair is found ("
                               + (sum - root.data) + ", "
                               + root.data + ")");
            return true;
        }
        else
            set.add(root.data);
 
        return findpairUtil(root.right, sum, set);
    }
 
    static void findPair(Node root, int sum)
    {
        HashSet set = new HashSet();
        if (!findpairUtil(root, sum, set))
            System.out.print("Pairs do not exit"
                             + "\n");
    }
 
    // Driver code
    public static void main(String[] args)
    {
        Node root = null;
        root = insert(root, 15);
        root = insert(root, 10);
        root = insert(root, 20);
        root = insert(root, 8);
        root = insert(root, 12);
        root = insert(root, 16);
        root = insert(root, 25);
        root = insert(root, 10);
 
        int sum = 33;
        findPair(root, sum);
    }
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to find a pair with
# given sum using hashing
import sys
import math
 
 
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
 
 
def insert(root, data):
    if root is None:
        return Node(data)
    if(data < root.data):
        root.left = insert(root.left, data)
    if(data > root.data):
        root.right = insert(root.right, data)
    return root
 
 
def findPairUtil(root, summ, unsorted_set):
    if root is None:
        return False
    if findPairUtil(root.left, summ, unsorted_set):
        return True
    if unsorted_set and (summ-root.data) in unsorted_set:
        print("Pair is Found ({},{})".format(summ-root.data, root.data))
        return True
    else:
        unsorted_set.add(root.data)
 
    return findPairUtil(root.right, summ, unsorted_set)
 
 
def findPair(root, summ):
    unsorted_set = set()
    if(not findPairUtil(root, summ, unsorted_set)):
        print("Pair do not exist!")
 
 
# Driver code
if __name__ == '__main__':
    root = None
    root = insert(root, 15)
    root = insert(root, 10)
    root = insert(root, 20)
    root = insert(root, 8)
    root = insert(root, 12)
    root = insert(root, 16)
    root = insert(root, 25)
    root = insert(root, 10)
    summ = 33
    findPair(root, summ)
 
# This code is contributed by Vikash Kumar 37


C#
// C# program to find a pair with
// given sum using hashing
using System;
using System.Collections.Generic;
 
class GFG {
 
    class Node {
        public int data;
        public Node left, right;
    };
 
    static Node NewNode(int data)
    {
        Node temp = new Node();
        temp.data = data;
        temp.left = null;
        temp.right = null;
        return temp;
    }
 
    static Node insert(Node root, int key)
    {
        if (root == null)
            return NewNode(key);
        if (key < root.data)
            root.left = insert(root.left, key);
        else
            root.right = insert(root.right, key);
        return root;
    }
 
    static bool findpairUtil(Node root, int sum,
                             HashSet set)
    {
        if (root == null)
            return false;
 
        if (findpairUtil(root.left, sum, set))
            return true;
 
        if (set.Contains(sum - root.data)) {
            Console.WriteLine("Pair is found ("
                              + (sum - root.data) + ", "
                              + root.data + ")");
            return true;
        }
        else
            set.Add(root.data);
 
        return findpairUtil(root.right, sum, set);
    }
 
    static void findPair(Node root, int sum)
    {
        HashSet set = new HashSet();
        if (!findpairUtil(root, sum, set))
            Console.Write("Pairs do not exit"
                          + "\n");
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        Node root = null;
        root = insert(root, 15);
        root = insert(root, 10);
        root = insert(root, 20);
        root = insert(root, 8);
        root = insert(root, 12);
        root = insert(root, 16);
        root = insert(root, 25);
        root = insert(root, 10);
 
        int sum = 33;
        findPair(root, sum);
    }
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:

Pair is found (8, 25)

时间复杂度: O(n)。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程