Skip to content

Commit 645feb3

Browse files
authored
Create main.cpp
1 parent 50ad262 commit 645feb3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution
2+
{
3+
public:
4+
void inOrder(Node* root, vector<Node*> &nodes){
5+
if(root == NULL) return;
6+
7+
inOrder(root -> left, nodes);
8+
nodes.push_back(root);
9+
inOrder(root -> right, nodes);
10+
}
11+
Node *flattenBST(Node *root)
12+
{
13+
if(root == NULL) return NULL;
14+
15+
vector<Node*> nodes;
16+
inOrder(root, nodes);
17+
18+
Node* newRoot = nodes[0];
19+
Node* current = newRoot;
20+
21+
for(int i = 1; i < nodes.size(); i++){
22+
current -> left = NULL;
23+
current -> right = nodes[i];
24+
current = nodes[i];
25+
}
26+
27+
current -> left = NULL;
28+
current -> right = NULL;
29+
30+
return newRoot;
31+
}
32+
};

0 commit comments

Comments
 (0)