How to traverse in a tree iterative C++
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
- array 2d to 1d
- Category
- C++
- Title
- vector pop back
- Category
- C++
- Title
- worker class c++
- Category
- C++
- Title
- c++ string^ to char*
- Category
- C++
- Title
- c++ convert int to cstring
- Category
- C++
- Title
- how to print 5 precision float in c++
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- c++ variable arguments
- Category
- C++
- Title
- how to print for limited decimal values in c++
- Category
- C++
- Title
- euler phi gfg
- Category
- C++
- Title
- c++ uint32_t
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- placement new c++
- Category
- C++
- Title
- how to ensure the user inouts a int and not anything else c++
- Category
- C++
- Title
- how to create object in c++
- Category
- C++
- Title
- merge sort in c++
- Category
- C++
- Title
- pyqt connect
- Category
- C++
- Title
- hashset in c++
- Category
- C++
- Title
- select elements from array C++
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- git branch in my bash prompt
- Category
- C++
- Title
- passing array to function in c++
- Category
- C++
- Title
- how to delete a node c++
- Category
- C++
- Title
- what is difference between ciel and floor
- Category
- C++
- Title
- conditional operator in cpp
- Category
- C++
- Title
- c++ unittest in ros
- Category
- C++
- Title
- c++ cli convert string to string^
- Category
- C++
- Title
- how to iterate trough a vector in c++
- Category
- C++
- Title
- container class in c++
- Category
- C++
- Title
- How to check if a triangular cycle exists in a graph
- Category
- C++
- Title
- c++ initialize a vector
- Category
- C++
- Title
- best fit algorithm
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- pause the console c++
- Category
- C++
- Title
- compare values within within a vector c++
- Category
- C++
- Title
- how to compare lower case character to uppercase cpp
- Category
- C++
- Title
- std::iomanip c++
- Category
- C++
- Title
- how to switch to another branch in git
- Category
- C++
- Title
- c++ short if
- Category
- C++
- Title
- findung the mode in c++
- Category
- C++
- Title
- c++ find object in vector by attribute
- Category
- C++
- Title
- mkdir boost filesystem
- Category
- C++
- Title
- how to print nth palindrome number in c++
- Category
- C++
- Title
- c++ convert int to double
- Category
- C++
- Title
- c++ compare char
- Category
- C++
- Title
- rick astley - never gonna give you up
- Category
- C++
- Title
- how to find the mode of a vector c++
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- power in c++
- Category
- C++
- Title
- making random numbers in c++
- Category
- C++
- Title
- static variable in c++
- Category
- C++
- Title
- substitution failure is not an error
- Category
- C++
- Title
- c++ empty stream
- Category
- C++
- Title
- c++ declare char
- Category
- C++
- Title
- c++ while true
- Category
- C++
- Title
- cloud hosting
- Category
- C++
- Title
- c++ switch
- Category
- C++
- Title
- class in c++
- Category
- C++
- Title
- what is order in of preeendence in float, int, char, bool
- Category
- C++
- Title
- quick sort predefined function in c++
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- matrix multiplication c++ eigen
- Category
- C++
- Title
- solve linear equations geeksforgeeks
- Category
- C++
- Title
- arduino falling edge
- Category
- C++
- Title
- comparing strings c++
- Category
- C++
- Title
- namespaces c++
- Category
- C++
- Title
- char* to int in cpp
- Category
- C++
- Title
- c++ program to find gcd of 3 numbers
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- hobo 8
- Category
- C++
- Title
- C++ and endl
- Category
- C++
- Title
- c++ function return pointer to itself
- Category
- C++
- Title
- getline in c++
- Category
- C++
- Title
- pass by reference c++
- Category
- C++
- Title
- c++ char to string
- Category
- C++
- Title
- c++ method name
- Category
- C++
- Title
- Check if a Number is Odd or Even using Bitwise Operators
- Category
- C++
- Title
- how to sort vector in c++
- Category
- C++
- Title
- c++ typeid get type name
- Category
- C++
- Title
- msdn parse command line
- Category
- C++
- Title
- insert elements in array in c++11
- Category
- C++
- Title
- case label in c++
- Category
- C++
- Title
- pow c++
- Category
- C++
- Title
- double to int c++
- Category
- C++
- Title
- object reference not set to an instance of an object c#
- Category
- C++
- Title
- checking an int in c++
- Category
- C++
- Title
- arrow operator c++
- Category
- C++
- Title
- reverse a linked list using recursion
- Category
- C++
- Title
- coping 2d vector in cpp
- Category
- C++
- Title
- how use global variables instead of local in c++
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- varint index
- Category
- C++
- Title
- dynamic 2d array c++
- Category
- C++
- Title
- declaring 2d vector in c++
- Category
- C++
- Title
- how to get a letter from the users string in c++
- Category
- C++
- Title
- iterate through unordered_map c++ in reverse order
- Category
- C++
- Title
- c++ vector pop_back
- Category
- C++
- Title
- c++ crash windows
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++