Bottom-Up Level Order of TreeMax Score: 100Given an array of unique elements, construct a Binary Search Tree and print the Level Order of the tree, not top-down, but bottom-up.Input FormatThe first line of input contains T - number of test cases. Its followed by 2T lines. First line of each test case contains N - number of nodes in the BST. The next line contains N unique integers - value of the nodes.Output FormatFor each test case, print the bottom-up Level Order of the Binary Search Tree, separate each level by newline. Separate the output of different test cases with an extra newline.Constraints1 <= T <= 10001 <= N <= 10000 <= ar[i] <= 10000ExampleInput351 2 3 4 553 2 4 1 574 5 15 0 1 7 17Output543211 52 437 171 150 54
Question
Bottom-Up Level Order of TreeMax Score: 100Given an array of unique elements, construct a Binary Search Tree and print the Level Order of the tree, not top-down, but bottom-up.Input FormatThe first line of input contains T - number of test cases. Its followed by 2T lines. First line of each test case contains N - number of nodes in the BST. The next line contains N unique integers - value of the nodes.Output FormatFor each test case, print the bottom-up Level Order of the Binary Search Tree, separate each level by newline. Separate the output of different test cases with an extra newline.Constraints1 <= T <= 10001 <= N <= 10000 <= ar[i] <= 10000ExampleInput351 2 3 4 553 2 4 1 574 5 15 0 1 7 17Output543211 52 437 171 150 54
Solution
This problem is about constructing a Binary Search Tree (BST) from an array of unique elements and printing the Level Order of the tree from bottom to top.
Here are the steps to solve this problem:
-
Parse the input: The first line of input contains T - the number of test cases. Each test case is followed by two lines. The first line contains N - the number of nodes in the BST. The second line contains N unique integers - the values of the nodes.
-
Construct the BST: For each test case, construct a BST using the given array of unique elements. A BST is a tree in which for each node, all elements in its left subtree are less than the node, and all elements in its right subtree are greater than the node.
-
Traverse the BST in Level Order from bottom to top: This can be done by using a queue and a stack. First, perform a regular level order traversal (top to bottom) and push each level into a stack. Then, pop each level from the stack and print it. This will give the level order from bottom to top.
-
Print the output: For each test case, print the bottom-up Level Order of the Binary Search Tree, separating each level by a newline. Separate the output of different test cases with an extra newline.
Constraints:
- 1 <= T <= 1000
- 1 <= N <= 1000
- 0 <= ar[i] <= 10000
Example: Input: 3 5 1 2 3 4 5 5 3 2 4 1 5 7 4 5 15 0 1 7 17
Output: 5 4 3 2 1
5 2 1 4
17 7 15 1 0 5 4
Similar Questions
Level Order of TreeGiven an array of unique elements, construct a Binary Search Tree and print the Level Order of the tree.Input FormatFirst line of each test case contains N - number of nodes in the BST. The next line contains N unique integers - value of the nodes.ConstraintsPrint the Level Order of the Binary Search Tree, separate each level by newline.Output Format1 <= N <= 10000 <= ar[i] <= 10000Sample input6 10 5 15 2 7 12Sample output10 5 15 2 7 12
Find preorder traversal of height balanced BSTGiven a sorted array. Convert it into a Height balanced Binary Search Tree (BST). Find the preorder traversal of height balanced BST. If there exist many such balanced BST consider the tree whose preorder is lexicographically smallest.Height balanced BST means a binary tree in which the depth of the left subtree and the right subtree of every node never differ by more than 1.Sample input25 1 4 5 7 8 12 14 15 19 20 22 25 26 27 28 29 33 34 37 40 41 42 43 47 48Sample outputoutput=26 12 5 1 4 7 8 19 14 15 22 20 25 37 2
Question4Max. score: 100.00Find Minimum and Maximum Values of BSTFind Minimum and MaximumWrite a relevant code snippet to print the maximum and minimum values of the binary search tree Sample Input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1 Sample Output:Minimum is : 5Maximum is : 75Sample input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1Sample output
Right View of TreeGiven an array of unique elements, construct a Binary Search Tree and print the right-view of the tree. Right view of a Tree is the set of nodes visible when tree is viewed from right side.Input FormatFirst line of each test case contains N - number of nodes in the BST. The next line contains N unique integers - value of the nodes.Constraints1 <= N <= 10000 <= ar[i] <= 10000Output FormatFor each test case, print the right-view of the Binary Search Tree, separated by newline.Sample input7 5 3 8 2 4 7 9Sample output5 8 9
VERTICAL ORDER TRAVERSALYou are given root of a binary tree, your task is to print the nodes in order obtained via vertical traversal of the tree.The vertical order traversal of a binary tree is a list of top-to-bottom orderings for each column index starting from the leftmost column and ending on the rightmost column. There may be multiple nodes in the same row and same column. In such a case, sort these nodes by their values.For each node at position (row, col), its left and right children will be at positions (row + 1, col - 1) and (row + 1, col + 1) respectively. The root of the tree is at (0, 0).Input format :The first line contains the number of test cases.For each test case: You are given a pointer to the root of the binary tree.Output format :Print the nodes in order obtained via vertical traversal of the tree. Refer the same Input/Output provided.Sample test cases :Input 1 :11 2 3Output 1 :2 1 3 Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.