Skip to content

Binary-Tree-Traversals #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Binary-Tree-Traversals
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Tree Node Structure */

struct Node {
int data;
Node* left;
Node* right;
};

/* Inorder Traversal of Binary tree */

void inorderTraversal(Node* root) {
if (root == NULL)
return;
/* recurring on left child */
inorderTraversal(root->left);

/* printing the data of node */
cout << root->data << " ";

/* recurring on right child */
inorderTraversal(root->right);
}

/* Preorder Traversal of Binary tree */

void preorderTraversal(Node* root) {
if (root == NULL)
return;
/* printing the data of node */
cout << root->data << " ";

/* recurring on left child */
preorderTraversal(root->left);

/* recurring on right child */
preorderTraversal(root->right);
}


/* Postorder Traversal of Binary tree */

void postorderTraversal(Node* root) {
if (root == NULL)
return;
/* recurring on left child */
postorderTraversal(root->left);

/* recurring on right child */
postorderTraversal(root->right);

/* printing the data of node */
cout << root->data << " ";
}