-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTop_View_of_Binary_Tree.cpp
More file actions
63 lines (45 loc) · 969 Bytes
/
Copy pathTop_View_of_Binary_Tree.cpp
File metadata and controls
63 lines (45 loc) · 969 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>
using namespace std;
unordered_map<int, int> um;
struct Node
{
int info;
struct Node *left, *right;
};
struct Node *create()
{
int data;
Node *tree;
tree = new Node;
cout << "\nEnter data to be inserted or type -1 : ";
cin >> data;
if (data == -1)
return 0;
tree->info = data;
cout << "Enter left child of " << data;
tree->left = create();
cout << "Enter right child of " << data;
tree->right = create();
return tree;
};
void topView(Node *root, int level)
{
if (root == NULL)
return;
int i = um.count(level);
if (i == 0)
um[level] = root->info;
topView(root->left, level - 1);
topView(root->right, level + 1);
um[level] = root->info;
return;
}
int main()
{
Node *root = NULL;
root = create();
topView(root, 0);
for (auto x : um)
cout << x.first << " --> " << x.second << endl;
return 0;
}