-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost_order_BT.cpp
More file actions
93 lines (90 loc) · 2.11 KB
/
post_order_BT.cpp
File metadata and controls
93 lines (90 loc) · 2.11 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct Node
{
int data;
struct Node* left;
struct Node* right;
};
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct Node* newNode(int data)
{
struct Node* node = new Node;
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
/* Computes the number of nodes in a tree. */
void postOrder(struct Node* root);
/* Driver program to test size function*/
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
map<int, Node*> m;
int n;
scanf("%d",&n);
Node *root = NULL;
while (n--)
{
Node *parent;
char lr;
int n1, n2;
scanf("%d %d %c", &n1, &n2, &lr);
// cout << n1 << " " << n2 << " " << (char)lr << endl;
if (m.find(n1) == m.end())
{
parent = newNode(n1);
m[n1] = parent;
if (root == NULL)
root = parent;
}
else
parent = m[n1];
Node *child = newNode(n2);
if (lr == 'L')
parent->left = child;
else
parent->right = child;
m[n2] = child;
}
postOrder(root);
cout << endl;
}
return 0;
}
/*Please note that it's Function problem i.e.
you need to write your solution in the form of Function(s) only.
Driver Code to call/invoke your function is mentioned above.*/
//User function Template for C++
/* A binary tree node has data, pointer to left child
and a pointer to right child
struct Node
{
int data;
Node* left;
Node* right;
}; */
/* Prints postorder traversal of Binary Tree. In output
all keys should be separated by space. For example
postorder traversal of below tree should be "20 30 10"
10
/ \
20 30 */
void postOrder(Node* root)
{
// Post -> L R Root
if(root == NULL)
return;
postOrder(root->left);
postOrder(root->right);
cout<<root->data<<" ";
// Your code here
}