1
+ // C++ Program to print Bottom View of Binary Tree
2
+ #include < bits/stdc++.h>
3
+ using namespace std ;
4
+ // Tree node class
5
+ struct Node
6
+ {
7
+ int data; // data of the node
8
+ Node *left, *right; // left and right references
9
+ // Constructor of tree node
10
+ Node (int key)
11
+ {
12
+ data = key;
13
+ left = right = NULL ;
14
+ }
15
+ };
16
+ // Method that prints the bottom view.
17
+ void bottomView (Node *root);
18
+ /* Driver program to test size function*/
19
+ int main ()
20
+ {
21
+ int t;
22
+ struct Node *child;
23
+ scanf (" %d" , &t);
24
+ while (t--)
25
+ {
26
+ map<int , Node*> m;
27
+ int n;
28
+ scanf (" %d" ,&n);
29
+ struct Node *root = NULL ;
30
+ while (n--)
31
+ {
32
+ Node *parent;
33
+ char lr;
34
+ int n1, n2;
35
+ scanf (" %d %d %c" , &n1, &n2, &lr);
36
+ if (m.find (n1) == m.end ())
37
+ {
38
+ parent = new Node (n1);
39
+ m[n1] = parent;
40
+ if (root == NULL )
41
+ root = parent;
42
+ }
43
+ else
44
+ parent = m[n1];
45
+ child = new Node (n2);
46
+ if (lr == ' L' )
47
+ parent->left = child;
48
+ else
49
+ parent->right = child;
50
+ m[n2] = child;
51
+ }
52
+ bottomView (root);
53
+ cout << endl;
54
+ }
55
+ return 0 ;
56
+ }
57
+
58
+
59
+ /* This is a function problem.You only need to complete the function given below*/
60
+ /* Tree node class
61
+ struct Node
62
+ {
63
+ int data; //data of the node
64
+ Node *left, *right; //left and right references
65
+ // Constructor of tree node
66
+ Node(int key)
67
+ {
68
+ data = key;
69
+ left = right = NULL;
70
+ }
71
+ }; */
72
+ // Method that prints the bottom view.
73
+
74
+ int height (Node* root)
75
+ {
76
+ int l =0 ,r=0 ;
77
+ if (root == NULL )
78
+ return 0 ;
79
+ if (root->left )
80
+ l = height (root->left );
81
+ if (root->right )
82
+ r = height (root->right );
83
+
84
+ return ((l>r)?l:r)+1 ;
85
+ }
86
+
87
+ void findbottomview (Node* root, int h, int current_distance, int width[])
88
+ {
89
+ if (root == NULL )
90
+ return ;
91
+
92
+ width[h+current_distance-1 ] = root->data ;
93
+ // cout<<"root data"<<root->data<< " and value is "<<width[h+current_distance-1]<<" also "<<h+current_distance-1;
94
+
95
+ if (root->left )
96
+ findbottomview (root->left , h, current_distance-1 , width);
97
+
98
+ if (root->right )
99
+ findbottomview (root->right , h, current_distance+1 , width);
100
+
101
+
102
+ }
103
+
104
+ void bottomView (Node *root)
105
+ {
106
+
107
+ if (root == NULL )
108
+ return ;
109
+ int totalheight = 0 ;
110
+ totalheight = height (root);
111
+ int h = 2 *totalheight -1 ;
112
+ int width[h];
113
+ // cout<<" totalheight & h = "<<totalheight<<h;
114
+ for (int i=0 ;i<h;i++)
115
+ {
116
+ width[i] = INT_MIN;
117
+ }
118
+ findbottomview (root,totalheight,0 ,width);
119
+
120
+ // cout<<"out of the loop";
121
+
122
+ for (int i=0 ;i<h;i++)
123
+ {
124
+ // cout<<" value of i "<<i<<" ";
125
+ if (width[i] == INT_MIN)
126
+ continue ;
127
+ cout<<width[i]<<" " ;
128
+ }
129
+ // Your Code Here
130
+ }
131
+
132
+
133
+ /*
134
+ For Input:
135
+ 2
136
+ 6
137
+ 0 1 L 0 2 R 1 3 L 1 4 R 2 5 L 2 6 R
138
+ 6
139
+ 0 1 L 0 2 R 1 3 L 1 4 R 2 5 L 2 6 R
140
+ */
0 commit comments