Build Binary Tree from BST such that it's level order traversal prints sorted data Last Updated : 13 Aug, 2021 Comments Improve Suggest changes Like Article Like Report Construct a Binary Tree from the given Binary Search Tree such that it's level order traversal outputs sorted data.Examples: Input: Output: 1 2 3 Input: Output: 1 2 3 4 5 Approach: Perform the inorder traversal of the given Binary Search Tree.Add each node in level order to the Binary Tree.Finally, print the level order traversal of the created Binary Tree. Below is the implementation of the above approach: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Structure to hold the contents // of the new node struct node { int data; node *left, *right; }* root1 = NULL; // Helper function to add and // return the newly added node node* add(int data) { node* newnode = new node; newnode->data = data; newnode->left = newnode->right = NULL; return newnode; } // Function to add a node to the // Binary Tree in the level order void addinBT(int data) { // If it is the first node // to be added then make // it the root node if (root1 == NULL) root1 = add(data); else { queue<node*> Q; Q.push(root1); while (!Q.empty()) { // Get and remove the front node* temp = Q.front(); Q.pop(); // If the left child of the current // node is null then create the new // node here and break if (temp->left == NULL) { temp->left = add(data); break; } else Q.push(temp->left); // If the right child of the current // node is null then create the new // node here and break if (temp->right == NULL) { temp->right = add(data); break; } else Q.push(temp->right); } } } // Function to add a node to // the Binary Search tree node* addinBST(node* root, int data) { // If the current node is null // then create a new node here // with the given data if (root == NULL) root = add(data); // If the data is smaller than the // current node's data then recur // for the left sub-tree else if (data < root->data) root->left = addinBST(root->left, data); // Else recur for the right sub-tree else root->right = addinBST(root->right, data); return root; } // Function to perform a level order // insertion in the Binary Tree from // the given Binary Search tree void addinorder(node* root) { if (root == NULL) return; addinorder(root->left); addinBT(root->data); addinorder(root->right); } // Function to print the level order // traversal of the binary tree void printlvl() { queue<node*> Q; // Push root to the queue Q.push(root1); while (!Q.empty()) { // Get the front node* temp = Q.front(); // Remove the front Q.pop(); // Print the data cout << temp->data << " "; // Push the left child if (temp->left != NULL) Q.push(temp->left); // Push the right child if (temp->right != NULL) Q.push(temp->right); } } // Driver code int main() { // Create the Binary Search Tree node* root = NULL; root = addinBST(root, 1); root = addinBST(root, 2); root = addinBST(root, 3); root = addinBST(root, 4); root = addinBST(root, 5); // Add nodes of the Binary Search // Tree to the Binary Tree addinorder(root); // Print the level order traversal // of the Binary Tree printlvl(); return 0; } Java // Java implementation of the approach import java.util.*; class GFG { // Structure to hold the contents // of the new node static class node { int data; node left, right; } static node root1 = null; // Helper function to add and // return the newly added node static node add(int data) { node newnode = new node(); newnode.data = data; newnode.left = newnode.right = null; return newnode; } // Function to add a node to the // Binary Tree in the level order static void addinBT(int data) { // If it is the first node // to be added then make // it the root node if (root1 == null) root1 = add(data); else { Queue<node> Q = new LinkedList<>(); Q.add(root1); while (!Q.isEmpty()) { // Get and remove the front node temp = Q.peek(); Q.remove(); // If the left child of the current // node is null then create the new // node here and break if (temp.left == null) { temp.left = add(data); break; } else Q.add(temp.left); // If the right child of the current // node is null then create the new // node here and break if (temp.right == null) { temp.right = add(data); break; } else Q.add(temp.right); } } } // Function to add a node to // the Binary Search tree static node addinBST(node root, int data) { // If the current node is null // then create a new node here // with the given data if (root == null) root = add(data); // If the data is smaller than the // current node's data then recur // for the left sub-tree else if (data < root.data) root.left = addinBST(root.left, data); // Else recur for the right sub-tree else root.right = addinBST(root.right, data); return root; } // Function to perform a level order // insertion in the Binary Tree from // the given Binary Search tree static void addinorder(node root) { if (root == null) return; addinorder(root.left); addinBT(root.data); addinorder(root.right); } // Function to print the level order // traversal of the binary tree static void printlvl() { Queue<node> Q = new LinkedList<>(); // Push root to the queue Q.add(root1); while (!Q.isEmpty()) { // Get the front node temp = Q.peek(); // Remove the front Q.remove(); // Print the data System.out.print(temp.data + " "); // Push the left child if (temp.left != null) Q.add(temp.left); // Push the right child if (temp.right != null) Q.add(temp.right); } } // Driver code public static void main(String[] args) { // Create the Binary Search Tree node root = null; root = addinBST(root, 1); root = addinBST(root, 2); root = addinBST(root, 3); root = addinBST(root, 4); root = addinBST(root, 5); // Add nodes of the Binary Search // Tree to the Binary Tree addinorder(root); // Print the level order traversal // of the Binary Tree printlvl(); } } // This code is contributed by Rajput-Ji Python3 # Python3 implementation of the approach # Structure to hold the contents # of the new node class add: # Constructor to create a new node def __init__(self, data): self.data = data self.left = self.right = None root1 = None # Function to add a node to the # Binary Tree in the level order def addinBT(data): global root1 # If it is the first node # to be added then make # it the root node if (root1 == None): root1 = add(data) else: Q = [root1] while (len(Q)): # Get and remove the front temp = Q[0] Q.pop(0) # If the left child of the current # node is None then create the new # node here and break if (temp.left == None): temp.left = add(data) break else: Q.append(temp.left) # If the right child of the current # node is None then create the new # node here and break if (temp.right == None): temp.right = add(data) break else: Q.append(temp.right) # Function to add a node to # the Binary Search tree def addinBST( root, data): # If the current node is None # then create a new node here # with the given data if (root == None): root = add(data) # If the data is smaller than the # current node's data then recur # for the left sub-tree elif (data < root.data): root.left = addinBST(root.left, data) # Else recur for the right sub-tree else: root.right = addinBST(root.right, data) return root # Function to perform a level order # insertion in the Binary Tree from # the given Binary Search tree def addinorder( root): if (root == None): return addinorder(root.left) addinBT(root.data) addinorder(root.right) # Function to print the level order # traversal of the binary tree def printlvl(): Q = [] # Push root to the Q.append(root1) while (len(Q)): # Get the front temp = Q[0] # Remove the front Q.pop(0) # Print the data print(temp.data ,end=" ") # Push the left child if (temp.left != None): Q.append(temp.left) # Push the right child if (temp.right != None): Q.append(temp.right) # Driver code # Create the Binary Search Tree root = add(1) root = addinBST(root, 2) root = addinBST(root, 3) root = addinBST(root, 4) root = addinBST(root, 5) # Add nodes of the Binary Search # Tree to the Binary Tree addinorder(root) # Print the level order traversal # of the Binary Tree printlvl() # This code is contributed by SHUBHAMSINGH10 C# // C# implementation of the approach using System; using System.Collections.Generic; class GFG { // Structure to hold the contents // of the new node public class node { public int data; public node left, right; } static node root1 = null; // Helper function to add and // return the newly added node static node add(int data) { node newnode = new node(); newnode.data = data; newnode.left = newnode.right = null; return newnode; } // Function to add a node to the // Binary Tree in the level order static void addinBT(int data) { // If it is the first node // to be added then make // it the root node if (root1 == null) root1 = add(data); else { Queue<node> Q = new Queue<node>(); Q.Enqueue(root1); while (Q.Count != 0) { // Get and remove the front node temp = Q.Peek(); Q.Dequeue(); // If the left child of the current // node is null then create the new // node here and break if (temp.left == null) { temp.left = add(data); break; } else Q.Enqueue(temp.left); // If the right child of the current // node is null then create the new // node here and break if (temp.right == null) { temp.right = add(data); break; } else Q.Enqueue(temp.right); } } } // Function to add a node to // the Binary Search tree static node addinBST(node root, int data) { // If the current node is null // then create a new node here // with the given data if (root == null) root = add(data); // If the data is smaller than the // current node's data then recur // for the left sub-tree else if (data < root.data) root.left = addinBST(root.left, data); // Else recur for the right sub-tree else root.right = addinBST(root.right, data); return root; } // Function to perform a level order // insertion in the Binary Tree from // the given Binary Search tree static void addinorder(node root) { if (root == null) return; addinorder(root.left); addinBT(root.data); addinorder(root.right); } // Function to print the level order // traversal of the binary tree static void printlvl() { Queue<node> Q = new Queue<node>(); // Push root to the queue Q.Enqueue(root1); while (Q.Count != 0) { // Get the front node temp = Q.Peek(); // Remove the front Q.Dequeue(); // Print the data Console.Write(temp.data + " "); // Push the left child if (temp.left != null) Q.Enqueue(temp.left); // Push the right child if (temp.right != null) Q.Enqueue(temp.right); } } // Driver code public static void Main(String[] args) { // Create the Binary Search Tree node root = null; root = addinBST(root, 1); root = addinBST(root, 2); root = addinBST(root, 3); root = addinBST(root, 4); root = addinBST(root, 5); // Add nodes of the Binary Search // Tree to the Binary Tree addinorder(root); // Print the level order traversal // of the Binary Tree printlvl(); } } // This code is contributed by Rajput-Ji JavaScript <script> // JavaScript implementation of the approach // Structure to hold the contents // of the new node class node { constructor() { this.data = 0; this.left = null; this.right = null; } } var root1 = null; // Helper function to add and // return the newly added node function add(data) { var newnode = new node(); newnode.data = data; newnode.left = newnode.right = null; return newnode; } // Function to add a node to the // Binary Tree in the level order function addinBT(data) { // If it is the first node // to be added then make // it the root node if (root1 == null) root1 = add(data); else { var Q = []; Q.push(root1); while (Q.Count != 0) { // Get and remove the front var temp = Q[0]; Q.shift(); // If the left child of the current // node is null then create the new // node here and break if (temp.left == null) { temp.left = add(data); break; } else Q.push(temp.left); // If the right child of the current // node is null then create the new // node here and break if (temp.right == null) { temp.right = add(data); break; } else Q.push(temp.right); } } } // Function to add a node to // the Binary Search tree function addinBST(root, data) { // If the current node is null // then create a new node here // with the given data if (root == null) root = add(data); // If the data is smaller than the // current node's data then recur // for the left sub-tree else if (data < root.data) root.left = addinBST(root.left, data); // Else recur for the right sub-tree else root.right = addinBST(root.right, data); return root; } // Function to perform a level order // insertion in the Binary Tree from // the given Binary Search tree function addinorder(root) { if (root == null) return; addinorder(root.left); addinBT(root.data); addinorder(root.right); } // Function to print the level order // traversal of the binary tree function printlvl() { var Q = []; // Push root to the queue Q.push(root1); while (Q.Count != 0) { // Get the front var temp = Q[0]; // Remove the front Q.shift(); // Print the data document.write(temp.data + " "); // Push the left child if (temp.left != null) Q.push(temp.left); // Push the right child if (temp.right != null) Q.push(temp.right); } } // Driver code // Create the Binary Search Tree var root = null; root = addinBST(root, 1); root = addinBST(root, 2); root = addinBST(root, 3); root = addinBST(root, 4); root = addinBST(root, 5); // Add nodes of the Binary Search // Tree to the Binary Tree addinorder(root); // Print the level order traversal // of the Binary Tree printlvl(); </script> Output: 1 2 3 4 5 Time Complexity: O(N).Auxiliary Space: O(N). Comment More infoAdvertise with us Next Article Build Binary Tree from BST such that it's level order traversal prints sorted data C code_freak Follow Improve Article Tags : Tree Technical Scripter Data Structures DSA Binary Tree tree-level-order +2 More Practice Tags : Data StructuresTree Similar Reads Construct Special Binary Tree from given Inorder traversal Given Inorder Traversal of a Special Binary Tree in which the key of every node is greater than keys in left and right children, construct the Binary Tree and return root. Examples: Input: inorder[] = {5, 10, 40, 30, 28} Output: root of following tree 40 / \ 10 30 / \ 5 28 Input: inorder[] = {1, 5, 14 min read Vertical order traversal of Binary Tree such that nodes are sorted individually Given a Binary Tree, the task is to find its vertical traversal starting from the leftmost level to the rightmost level. If multiple nodes pass through a vertical line, then print them in sorted order.Examples:Input: Output: 421 5 63 879Explanation: The below image shows the horizontal distances use 9 min read Print Binary Tree levels in sorted order | Set 3 (Tree given as array) Given a Complete Binary Tree as an array, the task is to print all of its levels in sorted order.Examples: Input: arr[] = {7, 6, 5, 4, 3, 2, 1} The given tree looks like 7 / \ 6 5 / \ / \ 4 3 2 1 Output: 7 5 6 1 2 3 4 Input: arr[] = {5, 6, 4, 9, 2, 1} The given tree looks like 5 / \ 6 4 / \ / 9 2 1 6 min read Binary Search Tree (BST) Traversals â Inorder, Preorder, Post Order Given a Binary Search Tree, The task is to print the elements in inorder, preorder, and postorder traversal of the Binary Search Tree. Input: A Binary Search TreeOutput: Inorder Traversal: 10 20 30 100 150 200 300Preorder Traversal: 100 20 10 30 200 150 300Postorder Traversal: 10 30 20 150 300 200 1 10 min read Level Order Traversal (Breadth First Search or BFS) of Binary Tree Given a Binary Tree, the task is to find its Level Order Traversal. Level Order Traversal technique is a method to traverse a Tree such that all nodes present in the same level are traversed completely before traversing the next level.Example:Input: Output: [[5], [12, 13], [7, 14, 2], [17, 23, 27, 3 14 min read Flatten Binary Tree in order of Level Order Traversal Given a Binary Tree, the task is to flatten it in order of Level order traversal of the tree. In the flattened binary tree, the left node of all the nodes must be NULL.Examples: Input: 1 / \ 5 2 / \ / \ 6 4 9 3 Output: 1 5 2 6 4 9 3 Input: 1 \ 2 \ 3 \ 4 \ 5 Output: 1 2 3 4 5 Approach: We will solve 7 min read Print Binary Tree levels in sorted order Given a Binary tree, the task is to print its all level in sorted order Examples: Input : 7 / \ 6 5 / \ / \ 4 3 2 1Output : 75 61 2 3 4 Input : 7 / \ 16 1 / \ 4 13 Output :7 1 164 13Recommended PracticePrint Binary Tree levels in sorted orderTry It!Here we can use two Priority queue for print in sor 9 min read Check if the given array can represent Level Order Traversal of Binary Search Tree Given an array of size n. The task is to check whether the given array can represent the level order traversal of a Binary Search Tree or not.Examples: Input: arr[] = {7, 4, 12, 3, 6, 8, 1, 5, 10}Output: TrueExplanation: For the given arr[] the Binary Search Tree is: Input: arr[] = {11, 6, 13, 5, 12 9 min read Perfect Binary Tree Specific Level Order Traversal | Set 2 Perfect Binary Tree using Specific Level Order Traversal in Set 1. The earlier traversal was from Top to Bottom. In this post, Bottom to Top traversal (asked in Amazon Interview | Set 120 â Round 1) is discussed. 16 31 17 30 18 29 19 28 20 27 21 26 22 25 23 24 8 15 9 14 10 13 11 12 4 7 5 6 2 3 1The 15+ min read Perfect Binary Tree Specific Level Order Traversal Given a Perfect Binary Tree like below: Print the level order of nodes in following specific manner: 1 2 3 4 7 5 6 8 15 9 14 10 13 11 12 16 31 17 30 18 29 19 28 20 27 21 26 22 25 23 24i.e. print nodes in level order but nodes should be from left and right side alternatively. Here 1st and 2nd levels 15+ min read Like