1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
convert-bst-to-greater-tree.cpp 739 Bytes
Copy Edit Raw Blame History
// Time: O(n)
// Space: O(h)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* convertBST(TreeNode* root) {
int cur_sum = 0;
convertBSTHelper(root, &cur_sum);
return root;
}
private:
void convertBSTHelper(TreeNode* root, int *cur_sum) {
if (!root) {
return;
}
if (root->right) {
convertBSTHelper(root->right, cur_sum);
}
root->val = (*cur_sum += root->val);
if (root->left) {
convertBSTHelper(root->left, cur_sum);
}
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

Search