-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #259 from nishantagrawal01/master
Binary-Tree-Traversals
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 << " "; | ||
} |