-
Notifications
You must be signed in to change notification settings - Fork 50
/
4-3.cpp
54 lines (45 loc) · 974 Bytes
/
4-3.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <queue>
using namespace std;
class TreeNode{
public:
TreeNode* left;
TreeNode* right;
int val;
TreeNode(int x):val(x),left(NULL),right(NULL){}
};
TreeNode* BST(int* A,int st, int ed){
if (st>ed){return NULL;}
else{
int mid = st+(ed-st)/2;
TreeNode* node = new TreeNode(A[mid]);
//cout << A[mid] << " ";
node->left = BST(A,st,mid-1);
node->right = BST(A,mid+1,ed);
return node;
}
}
//print a tree according to the level
void printTree(TreeNode* root){
queue<TreeNode*> q;
if (root==NULL){cout << "Empty Tree! "<< endl; return;}
q.push(root);
while (!q.empty()){
cout << q.front()->val << " ";
if (q.front()->left!=NULL){q.push(q.front()->left);}
if (q.front()->right!=NULL){q.push(q.front()->right);}
q.pop();
}
cout << endl;
}
int main(){
int n=13;
int* A = new int[n];
for (int i=0;i<n;i++){
A[i]=i;
}
TreeNode* root;
root = BST(A,0,n-1);
printTree(root);
return 0;
}