LeetCode653. 两数之和 IV

题目

直接暴力

 1 class Solution {
 2 public:
 3     vector<int>ans;
 4     bool findTarget(TreeNode* root, int k) {
 5         dfs(root);
 6         for(int i = 0;i <ans.size();i++)
 7             for(int j = i + 1;j <ans.size();j++){
 8                 if(ans[i] +ans[j] == k)
 9                     return true;
10             }
11         return false;
12 
13     }
14     void dfs(TreeNode* root){
15         if(root!=NULL){
16             dfs(root->left);
17             ans.push_back(root->val);
18             dfs(root->right);
19         }
20     }
21 };
原文地址:https://www.cnblogs.com/fresh-coder/p/14263048.html