#include <iostream>
using namespace std;
class TreeNode {
public :
int value;
TreeNode* left;
TreeNode* right;
TreeNode( int val) {
value = val;
left = right = nullptr;
}
} ;
TreeNode* insert( TreeNode* root, int value) {
// Base case: if the tree is empty, create a new node
if ( root == nullptr) {
return new TreeNode( value) ;
}
// Recursively find the correct position to insert the new value
if ( value < root- > value) {
root- > left = insert( root- > left, value) ; // Insert in the left subtree
} else {
root- > right = insert( root- > right, value) ; // Insert in the right subtree
}
return root; // Return the root of the tree
}
bool searchBST( TreeNode* root, int target) {
// Base case: if the root is null or we found the target
if ( root == nullptr) {
return false ;
}
if ( root- > value == target) {
return true ;
}
// Recursively search the left or right subtree based on the target
if ( target < root- > value) {
return searchBST( root- > left, target) ;
} else {
return searchBST( root- > right, target) ;
}
}
int main( ) {
TreeNode* root = nullptr;
int n, value, target;
cin >> n; // number of nodes
for ( int i = 0 ; i < n; i++ ) {
cin >> value; // node values
root = insert( root, value) ; // Insert the value into the tree
}
cin >> target; // target value to search
cout << ( searchBST( root, target) ? "true" : "false" ) << endl; // Search for the target
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3MgVHJlZU5vZGUgewpwdWJsaWM6CiAgICBpbnQgdmFsdWU7CiAgICBUcmVlTm9kZSogbGVmdDsKICAgIFRyZWVOb2RlKiByaWdodDsKCiAgICBUcmVlTm9kZShpbnQgdmFsKSB7CiAgICAgICAgdmFsdWUgPSB2YWw7CiAgICAgICAgbGVmdCA9IHJpZ2h0ID0gbnVsbHB0cjsKICAgIH0KfTsKClRyZWVOb2RlKiBpbnNlcnQoVHJlZU5vZGUqIHJvb3QsIGludCB2YWx1ZSkgewogICAgLy8gQmFzZSBjYXNlOiBpZiB0aGUgdHJlZSBpcyBlbXB0eSwgY3JlYXRlIGEgbmV3IG5vZGUKICAgIGlmIChyb290ID09IG51bGxwdHIpIHsKICAgICAgICByZXR1cm4gbmV3IFRyZWVOb2RlKHZhbHVl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