Skip to content

Instantly share code, notes, and snippets.

@Kishy-nivas
Created June 20, 2018 09:54
Show Gist options
  • Save Kishy-nivas/f99b06d94d81f589dd138242ebc4417e to your computer and use it in GitHub Desktop.
Save Kishy-nivas/f99b06d94d81f589dd138242ebc4417e to your computer and use it in GitHub Desktop.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool findTarget(TreeNode* root, int k) {
set<int> s;
return dfs(root,k,s);
}
bool dfs(TreeNode* root,int k,set<int>& s){
if(root == nullptr)
return false;
if(s.find(root->val ) != s.end())
return true;
s.insert(k-root->val );
return dfs(root->left ,k,s) || dfs(root->right,k,s);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment