class Solution {
public:
    bool findTarget(TreeNode* root, int k) {
        queue<TreeNode> Q;
        vector<int> V;
        if (root != NULL)
        {
            Q.push(*root);
            while (!Q.empty())
            {
                TreeNode livenode = Q.front();
                Q.pop();
                V.push_back(livenode.val);

                if (livenode.left != NULL)
                {
                    Q.push(*livenode.left);
                }
                if (livenode.right != NULL)
                {
                    Q.push(*livenode.right);
                }
            }

            sort(V.begin(), V.end());

            for (int i = 0; i < V.size(); i++)
            {
                for (int j = 0; j < V.size(); j++)
                {
                    int x = V[i];
                    int y = V[j];
                    if (x + y < k)
                    {
                        continue;
                    }
                    if (x + y == k&&i != j)
                    {
                        return true;
                    }
                    if (x + y > k)
                    {
                        break;
                    }
                }
            }
        }
        return false;
    }
};

 

posted on 2018-09-26 19:52  Sempron2800+  阅读(104)  评论(0编辑  收藏  举报