Given a binary tree and a key, insert the key into the binary tree at first position available in level order.
The idea is to do iterative level order traversal of the given tree using queue. If we find a node whose left child is empty, we make new key as left child of the node. Else if we find a node whose right child is empty, we make new key as right child. We keep traversing the tree until we find a node whose either left or right is empty.
Inorder traversal before insertion: 7 11 10 15 9 8 Inorder traversal after insertion: 7 11 12 10 15 9 8
This article is contributed by Yash Singla. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Insertion in n-ary tree in given order and Level order traversal
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Specific Level Order Traversal of Binary Tree
- Density of Binary Tree using Level Order Traversal
- Level Order Predecessor of a node in Binary Tree
- Level Order Successor of a node in Binary Tree
- Boundary Level order traversal of a Binary Tree
- Perfect Binary Tree Specific Level Order Traversal
- Deletion of a given node K in a Binary Tree using Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Print odd positioned nodes of even levels in level order of the given binary tree
- Print even positioned nodes of odd levels in level order of the given binary tree
- Print extreme nodes of each level of Binary Tree in alternate order
- Print even positioned nodes of even levels in level order of the given binary tree
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Check if the level order traversal of a Binary Tree results in a palindrome
- Check if value exists in level-order sorted complete binary tree
- Construct a complete binary tree from given array in level order fashion