forked from xperzy/careerup150
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-4.cpp
72 lines (60 loc) · 1.4 KB
/
4-4.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <queue>
#include <list>
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;
}
}
//store a tree according to the level
void storeTree(TreeNode* root, vector<vector<int> > &res){
queue<pair<TreeNode*, int> > q;
if (root==NULL){cout << "Empty Tree! "<< endl; return;}
int dep=0;
q.push(make_pair(root,dep));
vector<int> lev;
while (!q.empty()){
if (q.front().second!=dep){
res.push_back(lev);
lev.clear();
}
dep = q.front().second;
lev.push_back(q.front().first->val);
if (q.front().first->left!=NULL){q.push( make_pair(q.front().first->left,dep+1));}
if (q.front().first->right!=NULL){q.push( make_pair(q.front().first->right,dep+1));}
q.pop();
}
res.push_back(lev);
}
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);
vector<vector<int> > res;
storeTree(root,res);
for (int i=0;i<res.size();i++){
for (int j=0;j<res[i].size();j++){
cout << res[i][j]<<" ";
}
cout << endl;
}
return 0;
}