Skip to content
Open
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
66 changes: 66 additions & 0 deletions C++/DataStructure/Tree/inOrderTraversalIterative.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Author : Vishal Ambavade
Date : Date format 10/10/2021
Description : C++ program to print inorder traversal of tree using iterative approach.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mention Time, Space, and What is n

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @khagapati-bagh, I've added the time and space complexity. Please review it.

Time Complexity: O(n)
Space Complexity: O(n)
where n is the number of nodes in a binary tree
*/

#include <iostream>
// Using stack
#include <stack>

using namespace std;

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

Node(int data) {
this -> data = data;
left = right = NULL;
}
};

// Iterative function for inorder traversal of a tree
void inOrderTraversal(struct Node* root) {
stack<Node* > st;
Node* current = root;

while (current != NULL || st.empty() == false) {
//Go to the left most node of a tree
while (current != NULL) {
st.push(current);
current = current -> left;
}

current = st.top();
st.pop();

cout << current -> data << " ";

current = current -> right;
}
}

int main() {
struct Node* root = new Node(12);
root -> left = new Node(56);
root -> right = new Node(67);
root -> left -> left = new Node(23);
root -> left -> right = new Node(78);
root -> right -> left = new Node(32);

/* Counstructed binary tree:
12
/ \
56 67
/ \ /
23 78 32
*/

inOrderTraversal(root);
return 0;
}