leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1038.cpp (462B)
0 class Solution {
1 public:
2 TreeNode *bstToGst(TreeNode *root) {
3 TreeNode *head = root;
4 stack<TreeNode *> st;
5 int sum = 0;
6 while (true) {
7 while (root) {
8 st.push(root);
9 root = root->right;
10 }
11 if (st.empty()) break;
12 root = st.top(), st.pop();
13 sum = root->val += sum;
14 root = root->left;
15 }
16 return head;
17 }
18 };