-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranverse_binary_tree.cpp
58 lines (49 loc) · 1.07 KB
/
tranverse_binary_tree.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
#include <iostream>
#include <vector>
#include <map>
using namespace std;
struct Node
{
int data;
Node *left, *right;
};
Node *newNode(int key)
{
Node *node = new Node;
node->data = key;
node->left = node->right = NULL;
return node;
}
void getVerticalOrder(map<int, vector<int>> &m, int d, Node *root)
{
if (root == NULL)
{
return;
}
m[d].push_back(root->data);
getVerticalOrder(m, d--, root->left);
getVerticalOrder(m, d++, root->right);
return;
}
int main()
{
Node *root = newNode(10);
root->left = newNode(7);
root->left->left = newNode(3);
root->left->right = newNode(11);
root->right = newNode(4);
root->right->left = newNode(14);
root->right->right = newNode(16);
map<int, vector<int>> m;
int distance = 0;
getVerticalOrder(m, distance, root);
map<int, vector<int>>::iterator it;
for (it = m.begin(); it != m.end(); it++)
{
for(int i = 0; i<it->second.size(); i++){
cout << it->second[i] << " ";
}
cout<<endl;
}
return 0;
}