iterative inorder traversal
C++
// C++ program to print inorder traversal
// using stack.
#include<bits/stdc++.h>
using namespace std;
/* A binary tree Node has data, pointer to left child
and a pointer to right child */
struct Node
{
int data;
struct Node* left;
struct Node* right;
Node (int data)
{
this->data = data;
left = right = NULL;
}
};
/* Iterative function for inorder tree
traversal */
void inOrder(struct Node *root)
{
stack<Node *> s;
Node *curr = root;
while (curr != NULL || s.empty() == false)
{
/* Reach the left most Node of the
curr Node */
while (curr != NULL)
{
/* place pointer to a tree node on
the stack before traversing
the node's left subtree */
s.push(curr);
curr = curr->left;
}
/* Current must be NULL at this point */
curr = s.top();
s.pop();
cout << curr->data << " ";
/* we have visited the node and its
left subtree. Now, it's right
subtree's turn */
curr = curr->right;
} /* end of while */
}
/* Driver program to test above functions*/
int main()
{
/* Constructed binary tree is
1
/ \
2 3
/ \
4 5
*/
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);
inOrder(root);
return 0;
}
Also in C++:
- Title
- what are the different ways to traverse a binary tree
- Category
- C++
- Title
- ios_base::sync_with_stdio(false);cin.tie(NULL);
- Category
- C++
- Title
- double pointers C++
- Category
- C++
- Title
- what does the modularity mean in c++
- Category
- C++
- Title
- print type cpp
- Category
- C++
- Title
- mark occurances of elements in array cpp
- Category
- C++
- Title
- get data from terminal c++
- Category
- C++
- Title
- empty string c++ value
- Category
- C++
- Title
- c++ client service ros
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- how to convert int to string c++
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- variadic templates
- Category
- C++
- Title
- floor() in c++
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++
- Title
- cin.ignore
- Category
- C++
- Title
- find in set of pairs using first value cpp
- Category
- C++
- Title
- c++ main function
- Category
- C++
- Title
- quick sort predefined function in c++
- Category
- C++
- Title
- hobo 8
- Category
- C++
- Title
- hashset in c++
- Category
- C++
- Title
- calling a method on an object c++
- Category
- C++
- Title
- how to check datatype of a variable in c++
- Category
- C++
- Title
- function template
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- c++ how to skip the last element of vector
- Category
- C++
- Title
- factorion
- Category
- C++
- Title
- number of islands leetcode code
- Category
- C++
- Title
- how to remove maximum number of characters in c++ cin,ignore
- Category
- C++
- Title
- split 2d array into chunks in c++
- Category
- C++
- Title
- rand c++
- Category
- C++
- Title
- never gonna give you up lyrics
- Category
- C++
- Title
- max in c++
- Category
- C++
- Title
- coping 2d vector in cpp
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++
- Title
- Check if a Number is Odd or Even using Bitwise Operators
- Category
- C++
- Title
- error: redefinition of ‘class Customer’
- Category
- C++
- Title
- how to create object in c++
- Category
- C++
- Title
- c++ menu selection with arrow keys
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- sort function in cpp
- Category
- C++
- Title
- delete a double pointer c++
- Category
- C++
- Title
- C++ w3schools
- Category
- C++
- Title
- c++ uint32_t
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- memcmp in cpp
- Category
- C++
- Title
- input a string in c++
- Category
- C++
- Title
- pionter in c++
- Category
- C++
- Title
- trovare il valore massimo in un array c++ w3
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- how to calculate inverse trigonometric values in c++
- Category
- C++
- Title
- object slicing in c++
- Category
- C++
- Title
- restting a queue stl
- Category
- C++
- Title
- msdn parse command line
- Category
- C++
- Title
- zeros of array c++
- Category
- C++
- Title
- findung the mode in c++
- Category
- C++
- Title
- expected initializer before 'isdigit'|
- Category
- C++
- Title
- programa para saber si un numero es primo
- Category
- C++
- Title
- class is replace by structure
- Category
- C++
- Title
- c++ class inheritance
- Category
- C++
- Title
- dijkstra c++ geeksforgeeks using set
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- convert int to binary string c++
- Category
- C++
- Title
- how to get a letter from the user c++ string
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- lisy stl C++
- Category
- C++
- Title
- how to append two vectors in c++
- Category
- C++
- Title
- what is order in of preeendence in float, int, char, bool
- Category
- C++
- Title
- remove value from vector c++
- Category
- C++
- Title
- primeros numeors primos menores que
- Category
- C++
- Title
- c++ function to find length of array
- Category
- C++
- Title
- how to convert n space separated integers in c++
- Category
- C++
- Title
- first prime numbers less than
- Category
- C++
- Title
- compile c++ linux
- Category
- C++
- Title
- c++ constructor
- Category
- C++
- Title
- system("pause") note working c++
- Category
- C++
- Title
- conditional operator in cpp
- Category
- C++
- Title
- how to use wasd c++
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++
- Title
- set of vectors c++
- Category
- C++
- Title
- peak in c++
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- change int to string cpp
- Category
- C++
- Title
- c++ overloaded == operator
- Category
- C++
- Title
- c++ empty stream
- Category
- C++
- Title
- c++ switch
- Category
- C++
- Title
- how to decalre a string in c++
- Category
- C++
- Title
- checking an int in c++
- Category
- C++
- Title
- c++ sort
- Category
- C++
- Title
- Get handle in C++
- Category
- C++
- Title
- how to convert string into number
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- std::substring
- Category
- C++
- Title
- translate
- Category
- C++
- Title
- COnvert string to char * C++
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- c++ delete dynamically allocated array
- Category
- C++
- Title
- c++ char to string
- Category
- C++
- Title
- pop from between string c++
- Category
- C++