1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 11 static int wing=[]() 12 { 13 std::ios::sync_with_stdio(false); 14 cin.tie(NULL); 15 return 0; 16 }(); 17 18 class Solution 19 { 20 public: 21 unordered_set<int> s; 22 bool findTarget(TreeNode* root, int k) 23 { 24 if(root==NULL) 25 return false; 26 if(s.find(k-root->val)!=s.end()) 27 return true; 28 s.insert(root->val); 29 return findTarget(root->left,k)||findTarget(root->right,k); 30 } 31 };
可递归,可迭代,问题不大