Given an array of integers, the task is to construct a binary tree in level order fashion using Recursion.
Idea is to keep track of the number of child nodes in the left sub-tree and right sub-tree and then take the decision on the basis of these counts.
- When the count of children nodes in left and right sub-tree are equal, then the node has to be inserted in left sub-tree by creating a new level in the binary tree.
- When the count of children nodes in the left sub-tree is greater than the count of the children nodes in the right sub-tree then there are two cases.
- When the left sub-tree is perfect binary tree, then node is to be inserted in right sub-tree.
- When left sub-tree is not perfect binary tree, then node is to be inserted in left sub-tree.
A perfect binary tree with n levels have 2(n-1) nodes with all the leaf nodes at same level.
Below is the implementation of the above approach
12 6 5 8 1 7 9
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.
- Construct a complete binary tree from given array in level order fashion
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Flatten Binary Tree in order of Level Order Traversal
- Construct a tree from Inorder and Level order traversals | Set 1
- Construct BST from its given level order traversal
- Construct a tree from Inorder and Level order traversals | Set 2
- Construct BST from its given level order traversal | Set-2
- Find Maximum Level Sum in Binary Tree using Recursion
- Connect Nodes at same Level (Level Order Traversal)
- Insertion in n-ary tree in given order and Level order traversal
- Density of Binary Tree using Level Order Traversal
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Deletion of a given node K in a Binary Tree using Level Order Traversal
- Level order traversal of Binary Tree using Morris Traversal
- 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
- Product of nodes at k-th level in a tree represented as string using Recursion
- 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
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : 29AjayKumar