We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 50ad262 commit 645feb3Copy full SHA for 645feb3
18 - Binary Search Tree Data Structure Problems/13 - Flatten BST to Sorted List/main.cpp
@@ -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
28
+ current -> right = NULL;
29
30
+ return newRoot;
31
32
+};
0 commit comments