preorder traversal
C++
/* 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 << " ";
} // 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++ function return array
- Category
- C++
- Title
- how print fload wiht 3 decimal in c++
- Category
- C++
- Title
- pair c++
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- sqrt in c++
- Category
- C++
- Title
- c++ initialize array
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- Application of c++ in youtube program
- Category
- C++
- Title
- prims c++
- Category
- C++
- Title
- strchr function in c++
- Category
- C++
- Title
- c++ while true loop
- Category
- C++
- Title
- conditional operator in cpp
- Category
- C++
- Title
- c++ class member initialization
- Category
- C++
- Title
- namespace c++
- Category
- C++
- Title
- how to show c++ binary files in sublime text
- Category
- C++
- Title
- ue4 modular character
- Category
- C++
- Title
- c++ class inheritance
- Category
- C++
- Title
- for loop
- Category
- C++
- Title
- registering a new QML type
- Category
- C++
- Title
- how to run a c++ program in the background
- Category
- C++
- Title
- how to delete a node c++
- Category
- C++
- Title
- c++ wait for user input
- Category
- C++
- Title
- merge sort in c++
- Category
- C++
- Title
- binary search in set c++
- Category
- C++
- Title
- convert integer to string c++
- Category
- C++
- Title
- first prime numbers
- Category
- C++
- Title
- binary representation differ in bits
- Category
- C++
- Title
- 2927260.eps 2927262.jpg 2927263.ai License free.txt License premium.txt
- Category
- C++
- Title
- string comparison in c++
- Category
- C++
- Title
- insert at position in vector c++
- Category
- C++
- Title
- queue stl c++
- Category
- C++
- Title
- delete files c++
- Category
- C++
- Title
- find height of a tree
- Category
- C++
- Title
- prefix sum array
- Category
- C++
- Title
- runtime error in c++
- Category
- C++
- Title
- how to decalre a string in c++
- Category
- C++
- Title
- binary tree search
- Category
- C++
- Title
- c++ program for matrix addition
- Category
- C++
- Title
- how to output to console c++
- Category
- C++
- Title
- retu7rn this c++
- Category
- C++
- Title
- exponenciacion binaria
- Category
- C++
- Title
- iterative inorder traversal
- Category
- C++
- Title
- arrow operator c++
- Category
- C++
- Title
- binary search stl in c++
- Category
- C++
- Title
- how to sort a vector in c++
- Category
- C++
- Title
- c++ compiler for sublime text
- Category
- C++
- Title
- coronavirus
- Category
- C++
- Title
- object reference not set to an instance of an object c#
- Category
- C++
- Title
- self in c++
- Category
- C++
- Title
- sum of two numbers c++
- Category
- C++
- Title
- program to know if a number is prime
- Category
- C++
- Title
- case label in c++
- Category
- C++
- Title
- how to get the prime number in c++ where time complexity is 0(log n)
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- std::reverse
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- split string at index c++
- Category
- C++
- Title
- maximum possible number atmost k swaps
- Category
- C++
- Title
- transpose matrix eigen c++
- Category
- C++
- Title
- c++ delete dynamically allocated array
- Category
- C++
- Title
- fail() in c++
- Category
- C++
- Title
- ios_base::sync_with_stdio(false);cin.tie(NULL);
- Category
- C++
- Title
- mingw32/bin/ld.exe: C:\Users\mfrom\AppData\Local\Temp\ccSKcRks.o:PizzaPi.cpp:(.text$_ZN5PizzaC2Ev[__ZN5PizzaC2Ev]+0xa): undefined reference to `vtable for Pizza' collect2.exe: error: ld returned 1 exit status
- Category
- C++
- Title
- converting char to int in c++
- Category
- C++
- Title
- c++ formatting
- Category
- C++
- Title
- inserting an element in an set c++
- Category
- C++
- Title
- cs1955 unity vector3
- Category
- C++
- Title
- gcd in c++
- Category
- C++
- Title
- restting a queue stl
- Category
- C++
- Title
- console colors in C++
- Category
- C++
- Title
- max element in array c++ stl
- Category
- C++
- Title
- c++ while true
- Category
- C++
- Title
- copy a part of a vector in another in c++
- Category
- C++
- Title
- cout console
- Category
- C++
- Title
- how to iterate through a map in c++
- Category
- C++
- Title
- how to make a 2d vector in c++
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- convert string to stream c++
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- decimal to hex cpp
- Category
- C++
- Title
- system("pause") note working c++
- Category
- C++
- Title
- how to get input from the console in c++
- Category
- C++
- Title
- jump to case label c++
- Category
- C++
- Title
- c++ get type name of object
- Category
- C++
- Title
- int max in c++
- Category
- C++
- Title
- constant variables in c++
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- dijkstra c++ geeksforgeeks using set
- Category
- C++
- Title
- c++ how to loop through a vector but not the last element
- Category
- C++
- Title
- c++ garbage collection
- Category
- C++
- Title
- spicoli
- Category
- C++
- Title
- create a dictionary cpp
- Category
- C++
- Title
- sqrt cpp
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- pow c++
- Category
- C++
- Title
- c++ show time elapsed
- Category
- C++
- Title
- flake8 max line length
- Category
- C++
- Title
- c++ find object in vector by attribute
- Category
- C++
- Title
- user input c++
- Category
- C++