tree traversal c++ in order
C++
// C program for different tree traversals
#include <iostream>
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, *right;
Node(int data)
{
this->data = data;
left = right = NULL;
}
};
/* Given a binary tree, print its nodes according to the
"bottom-up" postorder traversal. */
void printPostorder(struct Node* node)
{
if (node == NULL)
return;
// first recur on left subtree
printPostorder(node->left);
// then recur on right subtree
printPostorder(node->right);
// now deal with the node
cout << node->data << " ";
}
/* Given a binary tree, print its nodes in inorder*/
void printInorder(struct Node* node)
{
if (node == NULL)
return;
/* first recur on left child */
printInorder(node->left);
/* then print the data of node */
cout << node->data << " ";
/* now recur on right child */
printInorder(node->right);
}
/* Given a binary tree, print its nodes in preorder*/
void printPreorder(struct Node* node)
{
if (node == NULL)
return;
/* first print data of node */
cout << node->data << " ";
/* then recur on left sutree */
printPreorder(node->left);
/* now recur on right subtree */
printPreorder(node->right);
}
/* Driver program to test above functions*/
int main()
{
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);
cout << "\nPreorder traversal of binary tree is \n";
printPreorder(root);
cout << "\nInorder traversal of binary tree is \n";
printInorder(root);
cout << "\nPostorder traversal of binary tree is \n";
printPostorder(root);
return 0;
}
Also in C++:
- Title
- c++ parse int
- Category
- C++
- Title
- first prime numbers
- Category
- C++
- Title
- compare values within within a vector c++
- Category
- C++
- Title
- date to string c++
- Category
- C++
- Title
- c++ while true
- Category
- C++
- Title
- c++ char define
- Category
- C++
- Title
- unsorted array to bst
- Category
- C++
- Title
- what is time complexity of insertion sort
- Category
- C++
- Title
- mysqli connect
- Category
- C++
- Title
- c++ vector lower_bound index
- Category
- C++
- Title
- nth_element c++
- Category
- C++
- Title
- rosrun actionlib_msgs genaction.py
- Category
- C++
- Title
- assegnare valori in c++
- Category
- C++
- Title
- C++ Student::Student()
- Category
- C++
- Title
- char* to int in cpp
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- c++ initialization list
- Category
- C++
- Title
- phph date
- Category
- C++
- Title
- c++ set add element
- Category
- C++
- Title
- c++ string to integer without stoi
- Category
- C++
- Title
- pop_back
- Category
- C++
- Title
- calling by reference and pointers c++
- Category
- C++
- Title
- what is meaning of 64 bit integer in c++
- Category
- C++
- Title
- root to leaf path print
- Category
- C++
- Title
- binary tree search
- Category
- C++
- Title
- how do for loops on c++
- Category
- C++
- Title
- powers of 2 in cpp
- Category
- C++
- Title
- how to compare two strings lexicographically in c++
- Category
- C++
- Title
- getting a random letter in c++
- Category
- C++
- Title
- string to vector c++
- Category
- C++
- Title
- how to type cast quotient of two integers to double with c++
- Category
- C++
- Title
- how to find the mode of a vector c++
- Category
- C++
- Title
- recursion in cpp with reference
- Category
- C++
- Title
- substitution failure is not an error
- Category
- C++
- Title
- C++ int to char*
- Category
- C++
- Title
- c++ remove element from vector
- Category
- C++
- Title
- sort function in c++
- Category
- C++
- Title
- number of islands leetcode code
- Category
- C++
- Title
- how to check datatype of a variable in c++
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- calling a method on an object c++
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- dijkstra c++ geeksforgeeks using set
- Category
- C++
- Title
- c++ delete dynamically allocated array
- Category
- C++
- Title
- double pointers C++
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- make an x using asterisk c++
- Category
- C++
- Title
- stack c++
- Category
- C++
- Title
- loop c++
- Category
- C++
- Title
- stringstream in c++ with delimiter
- Category
- C++
- Title
- split string at index c++
- Category
- C++
- Title
- git branch in my bash prompt
- Category
- C++
- Title
- what does count function do in hashmap
- Category
- C++
- Title
- expected unqualified-id before 'if'
- Category
- C++
- Title
- cannot jump from switch statement to this case label c++
- Category
- C++
- Title
- c++ push multiple elements to vector
- Category
- C++
- Title
- transpose matrix eigen c++
- Category
- C++
- Title
- minmax_element c++
- Category
- C++
- Title
- c++ overloaded == operator
- Category
- C++
- Title
- function template
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- list conda environments
- Category
- C++
- Title
- for loop
- Category
- C++
- Title
- Find the duplicate in an array of N integers.
- Category
- C++
- Title
- vector stl c++
- Category
- C++
- Title
- bfs in C++
- Category
- C++
- Title
- c++ dereference a pointer
- Category
- C++
- Title
- c++ get length of array
- Category
- C++
- Title
- registering a new QML type
- Category
- C++
- Title
- fmod c++
- Category
- C++
- Title
- how can make string value in cpp
- Category
- C++
- Title
- set precision in c++
- Category
- C++
- Title
- c++ ros publisher
- Category
- C++
- Title
- size of a matrix c++
- Category
- C++
- Title
- checking an int in c++
- Category
- C++
- Title
- declare vectors c++
- Category
- C++
- Title
- #include
- Category
- C++
- Title
- declaration vs. definition cpp
- Category
- C++
- Title
- how print fload wiht 3 decimal in c++
- Category
- C++
- Title
- min and max heap in cpp
- Category
- C++
- Title
- clear file before writing c++
- Category
- C++
- Title
- sum of integer in array c++
- Category
- C++
- Title
- singleton c++
- Category
- C++
- Title
- initialize 3d vector c++
- Category
- C++
- Title
- lambda c++
- Category
- C++
- Title
- C++ sfinae
- Category
- C++
- Title
- templates of templates c++
- Category
- C++
- Title
- string comparison in c++
- Category
- C++
- Title
- chess perft 5
- Category
- C++
- Title
- c++ stream string into fiel
- Category
- C++
- Title
- clear console c++
- Category
- C++
- Title
- type id c++
- Category
- C++
- Title
- find all the palindrome substring in a given string
- Category
- C++
- Title
- placement new c++
- Category
- C++
- Title
- std string find character c++
- Category
- C++
- Title
- pointer related problems dangling/wild pointers c++
- Category
- C++
- Title
- namespace file linking c++
- Category
- C++
- Title
- hobo 8
- Category
- C++
- Title
- c++ map insert
- Category
- C++
- Title
- Operator overloading in C++ Programming
- Category
- C++