iterative preorder traversal
C++
#include <bits/stdc++.h>
using namespace std;
/* A binary tree node has data, left child and right child */
struct node
{
int data;
struct node* left;
struct node* right;
};
/* Helper function that allocates a new node with the given data and
NULL left and right pointers.*/
struct node* newNode(int data)
{
struct node* node = new struct node;
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
// An iterative process to print preorder traversal of Binary tree
void iterativePreorder(node *root)
{
// Base Case
if (root == NULL)
return;
// Create an empty stack and push root to it
stack<node *> nodeStack;
nodeStack.push(root);
/* Pop all items one by one. Do following for every popped item
a) print it
b) push its right child
c) push its left child
Note that right child is pushed first so that left is processed first */
while (nodeStack.empty() == false)
{
// Pop the top item from stack and print it
struct node *node = nodeStack.top();
printf ("%d ", node->data);
nodeStack.pop();
// Push right and left children of the popped node to stack
if (node->right)
nodeStack.push(node->right);
if (node->left)
nodeStack.push(node->left);
}
}
// Driver program to test above functions
int main()
{
/* Constructed binary tree is
10
/ \
8 2
/ \ /
3 5 2
*/
struct node *root = newNode(10);
root->left = newNode(8);
root->right = newNode(2);
root->left->left = newNode(3);
root->left->right = newNode(5);
root->right->left = newNode(2);
iterativePreorder(root);
return 0;
}
Also in C++:
- Title
- how to concatinate two strings in c++
- Category
- C++
- Title
- c++ overloaded == operator
- Category
- C++
- Title
- convert char to string - c++
- Category
- C++
- Title
- split string at index c++
- Category
- C++
- Title
- zeros of array c++
- Category
- C++
- Title
- create a dictionary cpp
- Category
- C++
- Title
- loop c++
- Category
- C++
- Title
- get elements of 2d array c++
- Category
- C++
- Title
- powers of 2 in cpp
- Category
- C++
- Title
- c++ multiple inheritance diamond problem
- Category
- C++
- Title
- leveling system c++
- Category
- C++
- Title
- pyqt connect
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- delete files c++
- Category
- C++
- Title
- how to print for limited decimal values in c++
- Category
- C++
- Title
- transpose matrix eigen c++
- Category
- C++
- Title
- passing reference in c++
- Category
- C++
- Title
- tellg and seekg c++
- Category
- C++
- Title
- c++ clear console
- Category
- C++
- Title
- accumulate in cpp
- Category
- C++
- Title
- how to check type in c++
- Category
- C++
- Title
- empty string c++ value
- Category
- C++
- Title
- unordered_set in c++ and ordered set diff
- Category
- C++
- Title
- range of long long in c++
- Category
- C++
- Title
- level order traversal
- Category
- C++
- Title
- modular exponentiation c++
- Category
- C++
- Title
- singleton c++
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- sort function in c++
- Category
- C++
- Title
- Given an undirected graph, count the number of connected components.
- Category
- C++
- Title
- programa para saber si un numero es primo
- Category
- C++
- Title
- arduino delay millis
- Category
- C++
- Title
- removing repeated characters in a string c++
- Category
- C++
- Title
- how do for loops on c++
- Category
- C++
- Title
- how to compare lower case character to uppercase cpp
- Category
- C++
- Title
- c++ get type name of object
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- is TLE means my code is correct but taking more time to computr
- Category
- C++
- Title
- counting valleys hackerrank solution in c++
- Category
- C++
- Title
- loop through array c++
- Category
- C++
- Title
- rosrun actionlib_msgs genaction.py
- Category
- C++
- Title
- pionter in c++
- Category
- C++
- Title
- statement that causes a function to end in c++
- Category
- C++
- Title
- first fit algorithm
- Category
- C++
- Title
- map arduino
- Category
- C++
- Title
- how to output text in c++
- Category
- C++
- Title
- log base 10 c+_+
- Category
- C++
- Title
- vector concat c++
- Category
- C++
- Title
- hashset in c++
- Category
- C++
- Title
- char **
- Category
- C++
- Title
- calculate factorial
- Category
- C++
- Title
- c++ reset stream
- Category
- C++
- Title
- C++ user input
- Category
- C++
- Title
- c++ replace n substrings
- Category
- C++
- Title
- modulo c++
- Category
- C++
- Title
- 1d fixed length arrays c++
- Category
- C++
- Title
- char* to int in cpp
- Category
- C++
- Title
- what does map.count() return in c++
- Category
- C++
- Title
- char vector to string c++
- Category
- C++
- Title
- static variable in c++
- Category
- C++
- Title
- c++ char print width
- Category
- C++
- Title
- how print fload wiht 3 decimal in c++
- Category
- C++
- Title
- c++ movment
- Category
- C++
- Title
- range of int
- Category
- C++
- Title
- substitution failure is not an error
- Category
- C++
- Title
- double pointers C++
- Category
- C++
- Title
- solve linear equations geeksforgeeks
- Category
- C++
- Title
- body parser
- Category
- C++
- Title
- gfg left view of tree
- Category
- C++
- Title
- nth_element c++
- Category
- C++
- Title
- how to print eachh chars in string data type in c++
- Category
- C++
- Title
- c++ stack
- Category
- C++
- Title
- c++ char define
- Category
- C++
- Title
- multiset c++
- Category
- C++
- Title
- how to write an or in c++
- Category
- C++
- Title
- properties of a set c++
- Category
- C++
- Title
- rick astley - never gonna give you up
- Category
- C++
- Title
- c++ ros publisher
- Category
- C++
- Title
- if vector contains value c++
- Category
- C++
- Title
- ceil c++;
- Category
- C++
- Title
- run cmd command c++
- Category
- C++
- Title
- switch c++
- Category
- C++
- Title
- c++ main function
- Category
- C++
- Title
- c++ menu selection with arrow keys
- Category
- C++
- Title
- rgb(100,100,100,0.5) validation c++
- Category
- C++
- Title
- lambda operator in c++
- Category
- C++
- Title
- c++ client service ros
- Category
- C++
- Title
- how to measure program run time in c++
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- clear qlayout
- Category
- C++
- Title
- vector pop back
- Category
- C++
- Title
- how to sort a vector in c++
- Category
- C++
- Title
- c++ iterate over vector
- Category
- C++
- Title
- c++ raw string
- Category
- C++
- Title
- string to upper c++
- Category
- C++
- Title
- factorial in c++
- Category
- C++
- Title
- c++ multidimensional vector
- Category
- C++
- Title
- how to print nth palindrome number in c++
- Category
- C++
- Title
- setbits
- Category
- C++