leetcode653

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;
    }
};
原文地址:https://www.cnblogs.com/asenyang/p/9709190.html