Created
June 20, 2018 09:54
-
-
Save Kishy-nivas/f99b06d94d81f589dd138242ebc4417e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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