腾讯音乐互娱0427 | 笔试算法题c++版答案参考

删除树的某些层

坑点:要写 而不是

int vis[1000005];
vector<TreeNode*> deleteLevel(TreeNode* root, vector<int>& a) {
    memset(vis, 0, sizeof vis);
    vis[0] = 1;
    for (int x : a) vis[x] = 1;
    vector<TreeNode*> ans;
    queue<pair<TreeNode*, int>> q;
    q.push({root, 1});
    while (!q.empty()) {
        auto [r, dep] = q.front();
        q.pop();
        if (vis[dep - 1] == 1 and !vis[dep]) ans.push_back(r);
        if (r->left) q.push({r->left, dep + 1});
        if (r->right) q.push({r->right, dep + 1});
        if (vis[dep + 1] == 1)  r->left = r->right = NULL;
    }
    return ans;
}

对二叉树的子树做k次异或操作

int ope[100005];
void dfs(TreeNode* root, int now, int id) {
    now ^= ope[root->val];
    root->val = now;
    if (root->left) dfs(root->left, now, id * 2);
    if (root->right) dfs(root->right, now, id * 2 + 1);
}
TreeNode* xorTree(TreeNode* root, vector<vector<int> >& op) {
    memset(ope, 0, sizeof ope);
    for (vector<int> v : op) {
        int q = v[0], w = v[1];
        ope[q] = (ope[q] xor w);
    }
    dfs(root, 0, 1);
    return root;
}

字符计数

#腾讯音乐娱乐笔试##腾讯音乐娱乐#
全部评论

相关推荐

07-03 16:02
门头沟学院 Java
今天面试,非常紧张,面试官问我springboot有哪些核心模块都答不上来了,真的对自己无语了!
程序员小白条:28届我勒个去,很多人面试都没机会
查看1道真题和解析
点赞 评论 收藏
分享
05-26 10:24
门头沟学院 Java
qq乃乃好喝到咩噗茶:其实是对的,线上面试容易被人当野怪刷了
找工作时遇到的神仙HR
点赞 评论 收藏
分享
投递长鑫存储等公司8个岗位
点赞 评论 收藏
分享
评论
3
3
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务