Prerequisites: Binary Tree to Doubly Linked List
Input: Output: 7 8 5 9 10 Explanation: Required balanced binary tree will be: Input: Output: 13 21 29 7 15 Explanation: Required balanced binary tree is: 29 / \ 21 7 / \ 13 15
Follow the steps below to solve the problem:
- Find all the leaf nodes in the given binary tree and create a doubly linked list using them.
- To create Balanced Binary Tree from the above doubly linked list do the following:
- Find the middle node of the doubly linked list formed above and set it as a root node of the resultant tree.
- Recursively iterate for the left and right of the current middle node in the doubly linked list repeat the above steps until all nodes are covered.
- Print the the newly created balanced binary tree.
Below is the implementation of the above approach:
7 8 5 9 10
Time Complexity: O(N), where N is the number of nodes in the given tree.
Auxiliary Space Complexity: O(1)
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 XOR tree by Given leaf nodes of Perfect Binary Tree
- Sum of all leaf nodes of binary tree
- Count Non-Leaf nodes in a Binary Tree
- Product of all leaf nodes of binary tree
- Leaf nodes from Preorder of a Binary Search Tree
- Print all leaf nodes of a binary tree from right to left
- Print the nodes of binary tree as they become the leaf node
- Print all leaf nodes of a Binary Tree from left to right
- Maximum sum of leaf nodes among all levels of the given binary tree
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Program to count leaf nodes in a binary tree
- Remove all leaf nodes from the binary search tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Pairwise Swap leaf nodes in a binary tree
- Iterative program to count leaf nodes in a Binary Tree
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Print left and right leaf nodes separately in Binary Tree
- Print leaf nodes in binary tree from left to right using one stack
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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 : nidhi_biet