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.
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- 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
- Difference between sums of odd level and even level nodes of a Binary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Connect Nodes at same Level (Level Order Traversal)
- Perfect Binary Tree Specific Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Print extreme nodes of each level of Binary Tree in alternate order
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Construct a complete binary tree from given array in level order fashion
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Build Binary Tree from BST such that it's level order traversal prints sorted data
- Level Order Predecessor of a node in Binary Tree
- Level Order Successor of a node in Binary Tree
- Recursive Program to 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
- Density of Binary Tree using Level Order Traversal
- Print even positioned nodes of odd levels in level order of the given binary tree