Write a program to traverse a tree in Inorder sequence without recursion
#include <iostream>
using namespace std;
struct node {
int value;
struct node *left;
string node *right;
};
void inOrderTraversal(struct node *root) {
if (!root) return;
struct node *current = root;
while (current) {
if (!current->left) {
cout << current->value << " ";
current = current->right;
} else {
struct node *prev = current->left;
while (prev->right && prev->right != current) {
prev = prev->right;
}
if (!prev->right) {
prev->right = current;
current = current->left;
} else {
prev->right = NULL;
cout << current->value << " ";
current = current->right;
}
}
}
}
Comments
Leave a comment