Skip to content

Commit

Permalink
Time: 43 ms (56.88%), Space: 33.5 MB (64.32%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
gkapelyushok committed Apr 18, 2022
1 parent 5b703b1 commit c9cce5d
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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 Solution {
public:
TreeNode* convertBST(TreeNode* root) {
convert(root);
return root;
}

void convert(TreeNode* root) {
if (!root) {
return;
}
convert(root->right);
root->val += sum;
sum = root->val;
convert(root->left);
}

int sum = 0;
};

0 comments on commit c9cce5d

Please sign in to comment.