Simple c++ solution using map


  • 0
    T
    class Solution {
    public:
        unordered_map<int, int> m;
        bool findTarget(TreeNode* root, int k) {
            m.clear();
            return dfs(root, k);
        }
    
        bool dfs(TreeNode *root, int k) {
            if (!root) return false;
            if (m.find(k - root->val) != m.end()) return true;
            m[root->val]++;
            return dfs(root->left, k) || dfs(root->right, k);
        }
    };
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.