-
Notifications
You must be signed in to change notification settings - Fork 110
/
bst-traversal.cpp
39 lines (34 loc) · 1.03 KB
/
bst-traversal.cpp
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
29
30
31
32
33
34
35
36
37
38
39
#include <vector>
using namespace std;
class BST {
public:
int value;
BST* left;
BST* right;
BST(int val);
BST& insert(int val);
};
vector<int> inOrderTraverse(BST* tree, vector<int> array) {
// Write your code here.
if (!tree) return {};
if (tree->left) array = inOrderTraverse(tree->left, array);
array.push_back(tree->value);
if (tree->right) array = inOrderTraverse(tree->right, array);
return array;
}
vector<int> preOrderTraverse(BST* tree, vector<int> array) {
// Write your code here.
if (!tree) return {};
array.push_back(tree->value);
if (tree->left) array = preOrderTraverse(tree->left, array);
if (tree->right) array = preOrderTraverse(tree->right, array);
return array;
}
vector<int> postOrderTraverse(BST* tree, vector<int> array) {
// Write your code here.
if (!tree) return {};
if (tree->left) array = postOrderTraverse(tree->left, array);
if (tree->right) array = postOrderTraverse(tree->right, array);
array.push_back(tree->value);
return array;
}