-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreorder.cpp
More file actions
40 lines (32 loc) · 834 Bytes
/
Preorder.cpp
File metadata and controls
40 lines (32 loc) · 834 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
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include <vector>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int val) {
data = val;
left = right = NULL;
}
};
//static int indexPtr = -1; // FIX: "==" replaced with "=" and valid variable name
static int idx = -1;
Node* buildTree (vector<int>preorder){
idx++;
if (preorder[idx]==-1){
return NULL ;
}
Node* root = new Node(preorder[idx]);
root->left = buildTree(preorder); // left
root->right = buildTree(preorder); // right
return root;
}
int main (){
vector<int> preorder = {1,2,-1,-1,3,4,-1,-1,5,-1,-1};
Node* root = buildTree(preorder);
cout << root->data << endl;
cout << root-> left->data << endl;
cout << root->right->data << endl;
return 0;
}