Build Binary Tree in C++ (Competitive Programming)

Let’s start our journey of learning a hierarchical data structure (BINARY TREE) in C++. We will start from very basic of creating a binary tree with the help of class and functions. In this tutorial, we will learn how to build binary tree in C++. Before that just grab some information about basics of Binary tree.

   Build Binary Tree in C++ (Competitive Programming)


A binary tree comprises of parent nodes, or leaves, each of which stores data and also links to up to two other child nodes (leaves) which are visualized spatially as below the first node with one placed to the left and with one placed to the right.

Some of the important points of a binary tree are:-

  1. binary tree is made of nodes, where each node contains a left pointer (called the left subtree), a right pointer(called the right subtree), and a data element(data to be stored).
  2. The root pointer(node->root) points to the topmost node in the tree. A null pointer(NULL) represents a binary tree with no elements — >the empty tree.
  3. No order of data sorting is present in Binary tree.


It is generally represented as:

              	            /    \
	                   6      14
			  / \    /  \
		         5   8  11  18

The code is provided in C++ along with the essential information of the functions and classes. So, we created a class Node comprising of :Data to be stored, Left subtree pointer, Right subtree pointer, Parameterised constructor(node) along with two functions:-
1. Build tree
2. Print (taking the root node as an argument)

  • buildtree() function

The buildtree() inputs the value of data in variable d and root node is locally created with the data as d. The condition checks if the tree is empty (if empty return NULL) or not. The recursive call of function buildtree() is made on both left and right subtree of the binary tree and then root is returned.

  • print() function-

The print() function accepting root node is used to print the entire binary tree. If the tree is empty, no tree is printed. Else, the data of the root node is printed first followed by the recursive call of print function on both left and right subtree. A binary tree is build up and printed in main function by calling both functions.

Certainly the easiest code with optimized space and time complexity. This code is represented as Inorder traversal.

C++ Code – Inorder Traversal – Binary Tree

#include <iostream>
using namespace std;
class Node{
  int data;
  Node(int d){
Node* buildtree(){
  int d;
    return NULL;
  root=new Node(d);
  return root;
void print(Node*root){
  cout<<root->data<<" ";
int main()
  return 0;

Run this code online

3 4 -1 -1 5 -1 -1


3 4 5

You may also learn,

7 responses to “Build Binary Tree in C++ (Competitive Programming)”

  1. Shikha Srivastava says:

    For competitive programming which is more preferred using set in STL or using binary tree?

  2. Dolly Singh says:

    Well it all depends on the constraints within which you have to provide the solution.You can code by using sets in STL in optimized manner or can use binary trees with optimized functions to reduce complexity.It all depends on your knowledge of optimizing function use.Feel free to comment for further queries.

  3. pi says:

    Why is this note working for more than 7 nodes?

  4. ARPIT DAS says:

    The code is correct BUT the correction here will be that this is not a INORDER traversal. The concept of PREORDER traversal is used here to print the Binary Tree.

  5. Dolly says:

    Well we can use any traversal to print the tree instead of any specific traversal the output will be same.

  6. Himadri Shekhar Das says:

    1. The program while executing gives a blank screen as there was no provision for taking inputs.
    2. Creation and Traversal are two different operations.

  7. John Smith says:

    This solution is done in such a ad-hoc manner that it makes me wonder if this is some kind of joke played on reader. Why there is cin inside the build function? Couldn’t you pass a value you add to the tree and let build function take care of creating a new node and adding it to the tree? I guess that would have been more complex hence the writer has conveniently bypassed it altogether. Shitty code.

Leave a Reply

Your email address will not be published. Required fields are marked *