Skip to content
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

Tree Traversal Question issue no #44 #50

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions DSA/inorder_tree_traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
struct Node *left, *right;
Node(int v)
{
data = v;
left = right = NULL;
}
};

// inorder traversal function
void printInorder(struct Node *node)
{
if (node == NULL)
return;
// left subtree
printInorder(node->left);
// print the node data
cout << node->data << " ";
// right subtree
printInorder(node->right);
}

int main()
{
struct Node *root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->right = new Node(6);

printInorder(root);

return 0;
}
40 changes: 40 additions & 0 deletions DSA/preorder_tree_traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
struct Node *left, *right;
Node(int v)
{
data = v;
left = right = NULL;
}
};

// print preorder traversal function
void printPreorder(struct Node *node)
{
if (node == NULL)
return;
// print the node data
cout << node->data << " ";
// left subtree
printPreorder(node->left);
// right subtree
printPreorder(node->right);
}

int main()
{
struct Node *root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->right = new Node(6);

printPreorder(root);

return 0;
}