Skip to content

Commit 760bec0

Browse files
committed
Problem 175: Find if there exists a pair of nodes in BST, which add up to k
1 parent 842c5ed commit 760bec0

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 174 |
9+
| Total Problems | 175 |
1010

1111
</center>
1212

@@ -130,6 +130,7 @@ Include contains single header implementation of data structures and some algori
130130
| Given a Binary Search Tree, find ceil and floor of a given key in it. If the given key lie in the BST, then both floor and ceil is equal to that key, else ceil is equal to next greater key (if any) in the BST and floor is equal to previous greater key (if any) in the BST | [floor_ceil_bst.cpp](tree_problems/floor_ceil_bst.cpp) |
131131
| Find kth smallest element in a binary search tree | [kth_smallest.cpp](tree_problems/kth_smallest.cpp)|
132132
| Validate if a given binary tree is a binary search tree. | [validate_bst.cpp](tree_problems/validate_bst.cpp) |
133+
| Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.| [find_target_k.cpp](tree_problems/find_target_k.cpp) |
133134

134135
### String Problems
135136
| Problem | Solution |

tree_problems/find_target_k.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Given a Binary Search Tree and a target number,
3+
* return true if there exist two elements in the BST such that their sum is equal to the given target.
4+
*
5+
* Input:
6+
* 5
7+
* / \
8+
* 3 6
9+
* / \ \
10+
* 2 4 7
11+
*
12+
* Target = 9
13+
* Output: True
14+
*
15+
* Target = 28
16+
* Output: False
17+
*
18+
* Approach:
19+
*
20+
* Use a set to insert node values as we traverse the tree. If we find a node such that
21+
* k-current_node_val exist already in set, it means we have found a pair of nodes whose values adds up to k.
22+
* (i.e. current node and node with value k-current_node_val)
23+
*/
24+
25+
#include <iostream>
26+
#include <unordered_set>
27+
28+
struct TreeNode {
29+
int data;
30+
TreeNode* left;
31+
TreeNode* right;
32+
TreeNode(int d): data{d}, left{nullptr}, right{nullptr}{}
33+
};
34+
35+
bool find_target_k(TreeNode* root, int k, std::unordered_set<int>& set)
36+
{
37+
if (root == nullptr) {
38+
return false;
39+
}
40+
41+
if (set.find(k - root->data) != set.end()) {
42+
return true;
43+
}
44+
set.insert(root->data);
45+
46+
return find_target_k(root->left, k, set) ||
47+
find_target_k(root->right, k, set);
48+
}
49+
50+
bool find_target_k(TreeNode* root, int k)
51+
{
52+
std::unordered_set<int> set;
53+
return find_target_k(root, k, set);
54+
}
55+
56+
void print_inorder(TreeNode* root)
57+
{
58+
if (root != nullptr) {
59+
print_inorder(root->left);
60+
std::cout << root->data << " ";
61+
print_inorder(root->right);
62+
}
63+
}
64+
65+
int main()
66+
{
67+
TreeNode* root = new TreeNode(5);
68+
root->left = new TreeNode(3);
69+
root->right = new TreeNode(6);
70+
root->left->left = new TreeNode(2);
71+
root->left->right = new TreeNode(4);
72+
root->right->right = new TreeNode(7);
73+
74+
std::cout << "Inorder traversal of the current tree:";
75+
print_inorder(root);
76+
std::cout << std::endl;
77+
78+
if (find_target_k(root, 9)) {
79+
std::cout << "The tree contains two nodes which adds up to 9\n";
80+
} else {
81+
std::cout << "The tree does not contain two nodes which adds up to 9\n";
82+
}
83+
84+
if (find_target_k(root, 24)) {
85+
std::cout << "The tree contains two nodes which adds up to 24\n";
86+
} else {
87+
std::cout << "The tree does not contain two nodes which adds up to 24\n";
88+
}
89+
return 0;
90+
}

0 commit comments

Comments
 (0)