Binary Tree in C Using Recursion

Here you will get program to create binary tree in C using recursion.

What is Binary Tree?

A tree is said to be a binary tree if each node of the tree can have maximum of two children. Children of a node of binary tree are ordered. One child is called left child and the other is called right child. An example of binary tree is shown in below diagram.

Also Read: Binary Search Tree in C

C Program to Create a Binary Tree Using Recursion [Linked Representation]

Creation of Binary Tree Using Recursion

A binary tree can be created recursively. The program will work as follow:

  1. Read a data in x.
  2. Allocate memory for a new node and store the address in pointer p.
  3. Store the data x in the node p.
  4. Recursively create the left subtree of p and make it the left child of p.
  5. Recursively create the right subtree of p and make it the right child of p.
The program is written in C language which allows linked representation of binary tree. Code will be as follow:

Program to Create Binary Tree in C Using Recursion


Enter data(-1 for no data):5
Enter left child of 5:
Enter data(-1 for no data):7
Enter left child of 7:
Enter data(-1 for no data):8
Enter left child of 8:
Enter data(-1 for no data):3
Enter left child of 3:
Enter data(-1 for no data):-1
Enter right child of 3:
Enter data(-1 for no data):-1
Enter right child of 8:
Enter data(-1 for no data):-1
Enter right child of 7:
Enter data(-1 for no data):-1
Enter right child of 5:
Enter data(-1 for no data):-1

The preorder traversal of tree is:


In the above program I have used preorder traversal to just show that the tree is created properly or not. You can use any other traversal method here. Comment below if you found anything incorrect or missing in above program for binary tree in C.
Category: DSA

22 thoughts on “Binary Tree in C Using Recursion

  1. Arun M

    I got the answer.
    I just modified the program.
    Used cout & cin instead of printf & scanf.

    I got "malloc undeclared error".
    But i added

    Your program is nice 🙂

    Thank Youu

    1. Neeraj Mishra

      I am happy that my program was usefull for you. Thanks for visiting.

  2. Mark

    As far as I understand to create above binary tree ABCDEFG. your program needs input in the form
    A B D -1 -1 E -1 -1 C F -1 -1 G – 1-1
    Can you please provide a program to create ABCDEFG binary tree which take input in this order : A B C D E F G

    1. enes

      void inorder(node *t)
      inorder(t->left); //inorder traversal on left subtree
      printf(“\n%d”,t->data); // visit the root
      inorder(t->right); //inorder traversal om right subtree


      write this code and delete function preorder

      1. Yash

        Can u plz..explain this code I am confused.
        When the recursive function call of inorder(t->left) is done
        i.e when t->left=null(at the leftmost node)
        then if condition should no longer execute as t=null despite it is, why?

  3. Saurav Bhagat

    Thanks, I was searching for something like this only.

  4. Dinanath Sinha

    To be honest, I found this code a bit complicated and not properly arranged…what,s about the case if you want to insert a new node to the tree???..

    1. pulkit sharma

      well traversal function is a kind of function for displaying the tree elements.

  5. pulkit sharma

    Well its a good code, seriously helped a lot.Keep Doing the good Work!!!


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.