From 7869f3f66c201ee3dac6aaeef3b7c88d3a751ae2 Mon Sep 17 00:00:00 2001 From: Kratika Gupta <54476432+kratikagupta2002@users.noreply.github.com> Date: Wed, 26 Oct 2022 12:49:50 +0530 Subject: [PATCH] Post order binary tree traversal --- PostOrderTreeTraverersal.cpp | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 PostOrderTreeTraverersal.cpp diff --git a/PostOrderTreeTraverersal.cpp b/PostOrderTreeTraverersal.cpp new file mode 100644 index 0000000..f98aa04 --- /dev/null +++ b/PostOrderTreeTraverersal.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +struct Node { + int data; + struct Node *left, *right; +}; + +Node* newNode(int data) +{ + Node* temp = new Node; + temp->data = data; + temp->left = temp->right = NULL; + return temp; +} + +void printPostorder(struct Node* node) +{ + if (node == NULL) + return; + + printPostorder(node->left); + printPostorder(node->right); + cout << node->data << " "; +} + +int main() +{ + struct Node* root = newNode(1); + root->left = newNode(2); + root->right = newNode(3); + root->left->left = newNode(4); + root->left->right = newNode(5); + + printPostorder(root); + return 0; +}