what are the different ways to traverse a binary tree
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++ remove item from list
- Category
- C++
- Title
- c++ give options
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- log base e synthax c++
- Category
- C++
- Title
- what are the different ways to traverse a binary tree
- Category
- C++
- Title
- fizzbuzz in c++
- Category
- C++
- Title
- c++ reverse vector
- Category
- C++
- Title
- c++ crash windows
- Category
- C++
- Title
- retu7rn this c++
- Category
- C++
- Title
- newline in c++
- Category
- C++
- Title
- single line if c++
- Category
- C++
- Title
- popualte an array c++
- Category
- C++
- Title
- subarray sum in c++
- Category
- C++
- Title
- to_string c++
- Category
- C++
- Title
- unordered_map c++
- Category
- C++
- Title
- passing reference in c++
- Category
- C++
- Title
- c++ find prime numbers
- Category
- C++
- Title
- least number of coins to form a sum
- Category
- C++
- Title
- c++ remove space from string
- Category
- C++
- Title
- c++ char print width
- Category
- C++
- Title
- what is order in of preeendence in float, int, char, bool
- Category
- C++
- Title
- length of array in cpp
- Category
- C++
- Title
- how to make a heap using stl in c++
- Category
- C++
- Title
- c++ cout int
- Category
- C++
- Title
- can we compare a long long int with int in c++ using max or min functions
- 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
- how to read and write in a file c++
- Category
- C++
- Title
- making random numbers in c++
- Category
- C++
- Title
- c++ vector add element
- Category
- C++
- Title
- random number in c++
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- c++ short if
- Category
- C++
- Title
- C++ cin cout
- Category
- C++
- Title
- c++ menu selection with arrow keys
- Category
- C++
- Title
- removing repeated characters in a string c++
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- converting char to int in c++
- Category
- C++
- Title
- vector stl c++
- Category
- C++
- Title
- c++ declare variable
- Category
- C++
- Title
- initialize vector of vector c++
- Category
- C++
- Title
- compare function in sort c++ stl
- Category
- C++
- Title
- c++ code to print hello world
- Category
- C++
- Title
- sort a pair using c++ stl
- Category
- C++
- Title
- differentialble programming
- Category
- C++
- Title
- tellg and seekg c++
- Category
- C++
- Title
- c++ read file line by line
- Category
- C++
- Title
- c++ char to int
- Category
- C++
- Title
- repeating character in c++
- Category
- C++
- Title
- binary search stl in c++
- Category
- C++
- Title
- convert char to string - c++
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- file format not recognized treating as linker script c++
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- unordered_map c++ insert
- Category
- C++
- Title
- int random string generator c++
- Category
- C++
- Title
- primitive and non primitive data types in c++
- Category
- C++
- Title
- c++ max of array
- Category
- C++
- Title
- c++ char print align
- Category
- C++
- Title
- abs in c++
- Category
- C++
- Title
- how to remove maximum number of characters in c++ cin,ignore
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- timer in c++
- Category
- C++
- Title
- residuo en lenguaje c
- Category
- C++
- Title
- function for searching in map in c++
- Category
- C++
- Title
- c++ course
- Category
- C++
- Title
- fast io c++
- Category
- C++
- Title
- c++ vector size
- Category
- C++
- Title
- how to read a comma delimited file into an array c++
- Category
- C++
- Title
- min heap declaration in c++ stl
- Category
- C++
- Title
- class in c++
- Category
- C++
- Title
- filling 2d array with 0 c++
- Category
- C++
- Title
- generate random uniform distribution c++
- Category
- C++
- Title
- C++ pointer arithmetic
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- Read multiple files(.txt) c++
- Category
- C++
- Title
- const in c++
- Category
- C++
- Title
- c++ while true loop
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- convert int to binary string c++
- Category
- C++
- Title
- ue4 modular character
- Category
- C++
- Title
- euler phi gfg
- Category
- C++
- Title
- how to compile opencv c++ in ubuntu
- Category
- C++
- Title
- substr in c++
- Category
- C++
- Title
- c++ variable argument
- Category
- C++
- Title
- how to initialize 2d vector of any size
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- cout does not name a type
- Category
- C++
- Title
- c++ convert int to cstring
- Category
- C++
- Title
- c++ get ascii value of char
- Category
- C++
- Title
- COunt the number of continous subsequences such that the sum is between
- Category
- C++
- Title
- c++ server service ros
- Category
- C++
- Title
- c++ how to add something at the start of a vector
- Category
- C++
- Title
- sort function in c++
- Category
- C++
- Title
- arduino for command
- Category
- C++
- Title
- syntax c++
- Category
- C++
- Title
- get elements of 2d array c++
- Category
- C++
- Title
- iterative inorder traversal
- Category
- C++
- Title
- queue c++
- Category
- C++
- Title
- get line C++
- Category
- C++