Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define optimise ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
- struct Node {
- int data;
- Node* left;
- Node* right;
- };
- Node* createNode(int val) {
- Node* newNode = new Node;
- newNode->data = val;
- newNode->left = nullptr;
- newNode->right = nullptr;
- return newNode;
- }
- Node* insert(Node* root, int val) {
- if (root == nullptr) {
- return createNode(val);
- }
- if (val < root->data) {
- root->left = insert(root->left, val);
- } else {
- root->right = insert(root->right, val);
- }
- return root;
- }
- void postorderTraversal(Node* root) {
- if (root == nullptr) {
- return;
- }
- postorderTraversal(root->left);
- postorderTraversal(root->right);
- cout << root->data << endl;
- }
- int main() {
- Node* root = nullptr;
- int val;
- while(cin>>val){
- if(root == nullptr) root = insert(root, val);
- else insert(root, val);
- }
- postorderTraversal(root);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement