Skip to content

Instantly share code, notes, and snippets.

@winhtut
Last active March 1, 2022 15:51
Show Gist options
  • Save winhtut/13f68df03d0f511f875c1846dd7b82f8 to your computer and use it in GitHub Desktop.
Save winhtut/13f68df03d0f511f875c1846dd7b82f8 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
Node(int data) {
this->data = data;
left = right = NULL;
}
};
void postorder(struct Node* node) {
if (node == NULL)
return;
postorder(node->left);
postorder(node->right);
cout << node->data << "->";
}
void inorder(struct Node* node) {
if (node == NULL)
return;
inorder(node->left);
cout << node->data << "->";
inorder(node->right);
}
void preorder(struct Node* node) {
if (node == NULL)
return;
cout << node->data << "->";
preorder(node->left);
preorder(node->right);
}
int main() {
struct Node *root = new Node(8);
root->left = new Node(10);
root->right = new Node(3);
root->right->left = new Node(17);
root->left->left = new Node(4);
root->left->right = new Node(2);
cout << "Inorder traversal ";
inorder(root);
cout << "\nPreorder traversal ";
preorder(root);
cout << "\nPostorder traversal ";
postorder(root);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment