Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.
class Solution {
public:
TreeNode* convertBST(TreeNode* root) {
int pre = 0;
dfs(root, pre);
return root;
}
void dfs(TreeNode* root, int& pre) {
if (!root) {
return;
}
dfs(root->right, pre);
root->val += pre;
pre = root->val;
dfs(root->left, pre);
}
};