struct TreeNode{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int value):val(value),left(nullptr),right(nullptr){}
};
class SearchTreeNode
{
public:
vector<bool> bu;
bool searchRout(TreeNode*root,int target)
{
if(root==nullptr)
{
return false;
}
dfsRearch(root,target);
cout<<boolalpha<<bu[0];
cin.get();
return bu[0];
}
bool dfsRearch(TreeNode*root,int target)
{
if(root==nullptr)
{
return false;
}
int sum = 0;
sum+=root->val;
if(sum==target)
{
bu.push_back(true);
return true;
}
dfsRearch(root->left,target-(root->val));
dfsRearch(root->right,target-(root->val));
}
};
int main()
{
TreeNode* head = new TreeNode(5);
TreeNode* sec = new TreeNode(4);
TreeNode* thr = new TreeNode(11);
TreeNode* fou = new TreeNode(7);
TreeNode* fiv = new TreeNode(2);
head->left = sec;
sec->left = thr;
thr->left = fou;
thr->right = fiv;
SearchTreeNode s;
s.searchRout(head,22);
}