-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP114.cpp
More file actions
28 lines (27 loc) · 773 Bytes
/
P114.cpp
File metadata and controls
28 lines (27 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "header.h"
class Solution {
public:
void flatten(TreeNode* root) {
stack<TreeNode*> st;
TreeNode *last = nullptr;
st.push(root);
while (!st.empty()) {
TreeNode *p = st.top();
st.pop();
if (last) last->right = p;
while (p) {
if (p->right) st.push(p->right);
if (p->left) {
p->right = p->left;
p->left = nullptr;
p = p->right;
}
else
{
last = p;
break;
}
}
}
}
};