Skip to content

Commit f06964e

Browse files
Merge pull request #259 from nishantagrawal01/master
Binary-Tree-Traversals
2 parents 0ae5e7f + fe60661 commit f06964e

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Binary-Tree-Traversals

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* Tree Node Structure */
2+
3+
struct Node {
4+
int data;
5+
Node* left;
6+
Node* right;
7+
};
8+
9+
/* Inorder Traversal of Binary tree */
10+
11+
void inorderTraversal(Node* root) {
12+
if (root == NULL)
13+
return;
14+
/* recurring on left child */
15+
inorderTraversal(root->left);
16+
17+
/* printing the data of node */
18+
cout << root->data << " ";
19+
20+
/* recurring on right child */
21+
inorderTraversal(root->right);
22+
}
23+
24+
/* Preorder Traversal of Binary tree */
25+
26+
void preorderTraversal(Node* root) {
27+
if (root == NULL)
28+
return;
29+
/* printing the data of node */
30+
cout << root->data << " ";
31+
32+
/* recurring on left child */
33+
preorderTraversal(root->left);
34+
35+
/* recurring on right child */
36+
preorderTraversal(root->right);
37+
}
38+
39+
40+
/* Postorder Traversal of Binary tree */
41+
42+
void postorderTraversal(Node* root) {
43+
if (root == NULL)
44+
return;
45+
/* recurring on left child */
46+
postorderTraversal(root->left);
47+
48+
/* recurring on right child */
49+
postorderTraversal(root->right);
50+
51+
/* printing the data of node */
52+
cout << root->data << " ";
53+
}

0 commit comments

Comments
 (0)