Krononberg

1261. Find Elements in a Contaminated Binary Tree.cpp 본문

개발 로그/알고리즘

1261. Find Elements in a Contaminated Binary Tree.cpp

k._. 2021. 10. 8. 15:08

emplace vs insert..

insert는 container만들어서 삽입..

emplace는 X..

emplace가 더 효율적이다.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class FindElements {
public:
    set<int>s;
    void recover(TreeNode* root, int x){
        if(!root) return;
        root->val = x;
        s.emplace(x);
        recover(root->left, 2*x+1);
        recover(root->right, 2*x+2);
    }
    
    
    FindElements(TreeNode* root) {
        recover(root,0);
    }
    
    bool find(int target) {
        return s.count(target);
    }
};

/**
 * Your FindElements object will be instantiated and called as such:
 * FindElements* obj = new FindElements(root);
 * bool param_1 = obj->find(target);
 */