VOOZH about

URL: https://www.geeksforgeeks.org/dsa/insertion-in-a-binary-tree-in-level-order/

⇱ Insertion in a Binary Tree in level order - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Insertion in a Binary Tree in level order

Last Updated : 4 Dec, 2025

Given a binary tree and a key, the task is to insert the key into the binary tree at the first position available in level order manner.

Examples:

Input: key = 12

👁 insertion-in-a-binary-tree-in-level-order

Output:

👁 insertion-in-a-binary-tree-in-level-order-2


Explanation: Node with value 12 is inserted into the binary tree at the first position available in level order manner.

Approach:

The idea is to do an iterative level order traversal of the given tree using queue. If we find a node whose left child is empty, we make a new key as the left child of the node. Else if we find a node whose right child is empty, we make the new key as the right child. We keep traversing the tree until we find a node whose either left or right child is empty

👁 Insertion-in-Binary-Tree

Below is the implementation of the above approach: 


Output
7 11 12 10 15 9 8 

Time Complexity:  O(n) where n is the number of nodes.
Auxiliary Space: O(n)

Comment
Article Tags:
Article Tags: