腾讯音乐互娱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; }
字符计数
略
#腾讯音乐娱乐笔试##腾讯音乐娱乐#