level order traversal
C++
// Recursive CPP program for level
// order traversal of Binary Tree
#include <bits/stdc++.h>
using namespace std;
/* A binary tree node has data,
pointer to left child
and a pointer to right child */
class node
{
public:
int data;
node* left, *right;
};
/* Function protoypes */
void printGivenLevel(node* root, int level);
int height(node* node);
node* newNode(int data);
/* Function to print level
order traversal a tree*/
void printLevelOrder(node* root)
{
int h = height(root);
int i;
for (i = 1; i <= h; i++)
printGivenLevel(root, i);
}
/* Print nodes at a given level */
void printGivenLevel(node* root, int level)
{
if (root == NULL)
return;
if (level == 1)
cout << root->data << " ";
else if (level > 1)
{
printGivenLevel(root->left, level-1);
printGivenLevel(root->right, level-1);
}
}
/* Compute the "height" of a tree -- the number of
nodes along the longest path from the root node
down to the farthest leaf node.*/
int height(node* node)
{
if (node == NULL)
return 0;
else
{
/* compute the height of each subtree */
int lheight = height(node->left);
int rheight = height(node->right);
/* use the larger one */
if (lheight > rheight)
return(lheight + 1);
else return(rheight + 1);
}
}
/* Helper function that allocates
a new node with the given data and
NULL left and right pointers. */
node* newNode(int data)
{
node* Node = new node();
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return(Node);
}
/* Driver code*/
int main()
{
node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
cout << "Level Order traversal of binary tree is \n";
printLevelOrder(root);
return 0;
}
// This code is contributed by rathbhupendra
Also in C++:
- Title
- length of string c++
- Category
- C++
- Title
- FInd the element which appears more than n/2 times C++
- Category
- C++
- Title
- gcd in c++
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- c++ remove item from list
- Category
- C++
- Title
- cout console
- Category
- C++
- Title
- residuo en lenguaje c
- Category
- C++
- Title
- c++ find object in vector by attribute
- Category
- C++
- Title
- c++ typeid get type name
- Category
- C++
- Title
- never gonna give you up lyrics
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- ceil c++;
- Category
- C++
- Title
- how to cout in c++
- Category
- C++
- Title
- vector pop back
- Category
- C++
- Title
- foind th output c++
- Category
- C++
- Title
- placement new c++
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- is TLE means my code is correct but taking more time to computr
- Category
- C++
- Title
- how to delete something in an array c++
- Category
- C++
- Title
- is not a nonstatic data member or base class of class
- Category
- C++
- Title
- two sum problem in c++
- Category
- C++
- Title
- how to print a string to console in c++
- Category
- C++
- Title
- how to ensure the user inouts a int and not anything else c++
- Category
- C++
- Title
- set lower bound c++
- Category
- C++
- Title
- matrix class in c++
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++
- Title
- c++ get last element in vector
- Category
- C++
- Title
- how to create object in c++
- Category
- C++
- Title
- std::iomanip c++
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- how to measure program run time in c++
- Category
- C++
- Title
- declaring 2d vector in c++
- Category
- C++
- Title
- c++ uint32_t
- Category
- C++
- Title
- how to round to nearest whole number unity
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- c++ code to print hello world
- Category
- C++
- Title
- constant variables in c++
- Category
- C++
- Title
- c++ do you not inherit constructor
- Category
- C++
- Title
- powers of 2 in cpp
- Category
- C++
- Title
- pass by reference c++
- Category
- C++
- Title
- how to convert int to string c++
- Category
- C++
- Title
- first prime numbers less than
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- getting a random letter in c++
- Category
- C++
- Title
- what does count function do in hashmap
- Category
- C++
- Title
- insert function in c++ vector
- Category
- C++
- Title
- calling by reference c++
- Category
- C++
- Title
- variabili in c++
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- c++ overload operator
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- % operator in c++
- Category
- C++
- Title
- c++ string to vector int
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- expected initializer before 'isdigit'|
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- c++ delete dynamically allocated array
- Category
- C++
- Title
- count number of zeros in array in O(logN)
- Category
- C++
- Title
- pyqt connect
- Category
- C++
- Title
- c++ get type name of object
- Category
- C++
- Title
- c++ while true
- Category
- C++
- Title
- binary tree deletion
- Category
- C++
- Title
- c++ program to find gcd of 3 numbers
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- matrix eigen c++ example
- Category
- C++
- Title
- inserting an element in an set c++
- Category
- C++
- Title
- ue4 c++ struct
- Category
- C++
- Title
- Combination Sum
- Category
- C++
- Title
- new c++
- Category
- C++
- Title
- heap in cpp stl
- Category
- C++
- Title
- how to calculate trigonometric values in c++
- Category
- C++
- Title
- bellman ford algorithm cp algorithm
- Category
- C++
- Title
- how to get the largest number in a c++ array
- Category
- C++
- Title
- how to input multiple lines of a file in c++
- Category
- C++
- Title
- double to float c++
- Category
- C++
- Title
- removing repeated characters in a string c++
- Category
- C++
- Title
- c++ crash windows
- Category
- C++
- Title
- number of islands leetcode code
- Category
- C++
- Title
- c++ char define
- Category
- C++
- Title
- find all the palindrome substring in a given string
- Category
- C++
- Title
- what is a header in c++
- Category
- C++
- Title
- char size length c++
- Category
- C++
- Title
- how to use wasd c++
- Category
- C++
- Title
- passing reference in c++
- Category
- C++
- Title
- c++ smart pointer 2d array
- Category
- C++
- Title
- c++ client service ros
- Category
- C++
- Title
- cs1955 unity vector3
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- how to check datatype of a variable in c++
- Category
- C++
- Title
- *max_element in c++
- Category
- C++
- Title
- c++ program to input and print text using Dynamic Memory Allocation.loop
- Category
- C++
- Title
- c++ rainbow text
- Category
- C++
- Title
- how to use max_element in c++ with vector
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- check an stack is empty c++
- Category
- C++
- Title
- how to delete a node c++
- Category
- C++