`{"id":"adf96d95-db47-42fc-bcfb-1afc586f6987","name":"Construct Bst From Levelorder Traversal","description":"1. You are given a partially written function to solve(Refer question video).\r\n2. Task : Construct Binary Search Tree from given LevelOrder Traversal.\r\n3. you will be given an array representing a valid LevelOrder of a Binary Search Tree. Program is required to create a unique Binary Search Tree.\r\n","inputFormat":"Input is managed for you.\r\n","outputFormat":"Output is managed for you.\r\n","constraints":"0 &lt;= Number of Nodes &lt;= 10^9\r\n-10^9 &lt;= value of Node data &lt;= 10^9\r\nValid LevelOrder traversal.","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\n#include <queue>\r\nusing namespace std;\r\n\r\nclass TreeNode\r\n{\r\npublic:\r\n int val = 0;\r\n TreeNode *left = nullptr;\r\n TreeNode *right = nullptr;\r\n\r\n TreeNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nclass levelPair\r\n{\r\npublic:\r\n TreeNode *par = nullptr;\r\n int lb = -(int)1e8;\r\n int rb = (int)1e8;\r\n\r\n levelPair()\r\n {\r\n }\r\n\r\n levelPair(TreeNode *par, int lb, int rb)\r\n {\r\n this->par = par;\r\n this->lb = lb;\r\n this->rb = rb;\r\n }\r\n};\r\n\r\nTreeNode *constructBSTFromLevelOrder(vector<int> &arr)\r\n{\r\n return nullptr;\r\n}\r\n\r\n// input_Section_====================================================================\r\n\r\nvoid display(TreeNode *node)\r\n{\r\n if (node == nullptr)\r\n return;\r\n\r\n string str = \"\";\r\n str += ((node->left != nullptr ? to_string(node->left->val) : \".\"));\r\n str += (\" -> \" + to_string(node->val) + \" <- \");\r\n str += ((node->right != nullptr ? to_string(node->right->val) : \".\"));\r\n\r\n cout << str << endl;\r\n\r\n display(node->left);\r\n display(node->right);\r\n}\r\n\r\nvoid solve()\r\n{\r\n int n;\r\n cin >> n;\r\n vector<int> pre(n, 0);\r\n for (int i = 0; i < n; i++)\r\n {\r\n cin >> pre[i];\r\n }\r\n\r\n TreeNode *root = constructBSTFromLevelOrder(pre);\r\n display(root);\r\n}\r\n\r\nint main()\r\n{\r\n solve();\r\n return 0;\r\n}"},"java":{"code":"import java.util.Scanner;\r\n\r\npublic class Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class TreeNode {\r\n int val = 0;\r\n TreeNode left = null;\r\n TreeNode right = null;\r\n\r\n TreeNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static TreeNode constructBSTFromLevelOrder(int[] LevelOrder) {\r\n return null;\r\n }\r\n\r\n // input_section=================================================\r\n\r\n public static void display(TreeNode node) {\r\n if (node == null)\r\n return;\r\n\r\n StringBuilder sb = new StringBuilder();\r\n sb.append((node.left != null ? node.left.val : \".\"));\r\n sb.append(\" -> \" + node.val + \" <- \");\r\n sb.append((node.right != null ? node.right.val : \".\"));\r\n\r\n System.out.println(sb.toString());\r\n\r\n display(node.left);\r\n display(node.right);\r\n\r\n }\r\n\r\n public static void solve() {\r\n int n = scn.nextInt();\r\n int[] level = new int[n];\r\n for (int i = 0; i < n; i++)\r\n level[i] = scn.nextInt();\r\n\r\n TreeNode root = constructBSTFromLevelOrder(level);\r\n display(root);\r\n }\r\n\r\n public static void main(String[] args) {\r\n solve();\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"6\r\n3 2 5 1 6 7","sampleOutput":"2 -> 3 <- 5\r\n1 -> 2 <- .\r\n. -> 1 <- .\r\n. -> 5 <- 6\r\n. -> 6 <- 7\r\n. -> 7 <- .\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"2e9df04c-be14-441c-9a18-8b5a8ca6596d","name":"Trees For Intermediate","slug":"trees-for-intermediate-9994","type":0},{"id":"cd55418f-46a3-4df8-a904-614a73b595c6","name":"Construct Bst From Levelorder Traversal","slug":"construct-bst-from-levelorder-traversal","type":1}],"next":{"id":"be3e37ca-501e-48bc-afc1-aea3844f3540","name":"Construct Bst From Levelorder Traversal","type":3,"slug":"construct-bst-from-levelorder-traversal"},"prev":{"id":"389f5053-eca7-4f5b-9949-9c54ebe3d87c","name":"Right View Of A Binarytree","type":0,"slug":"right-view-of-a-binarytree"}}}`

# Construct Bst From Levelorder Traversal

1. You are given a partially written function to solve(Refer question video). 2. Task : Construct Binary Search Tree from given LevelOrder Traversal. 3. you will be given an array representing a valid LevelOrder of a Binary Search Tree. Program is required to create a unique Binary Search Tree.

`{"id":"adf96d95-db47-42fc-bcfb-1afc586f6987","name":"Construct Bst From Levelorder Traversal","description":"1. You are given a partially written function to solve(Refer question video).\r\n2. Task : Construct Binary Search Tree from given LevelOrder Traversal.\r\n3. you will be given an array representing a valid LevelOrder of a Binary Search Tree. Program is required to create a unique Binary Search Tree.\r\n","inputFormat":"Input is managed for you.\r\n","outputFormat":"Output is managed for you.\r\n","constraints":"0 &lt;= Number of Nodes &lt;= 10^9\r\n-10^9 &lt;= value of Node data &lt;= 10^9\r\nValid LevelOrder traversal.","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\n#include <queue>\r\nusing namespace std;\r\n\r\nclass TreeNode\r\n{\r\npublic:\r\n int val = 0;\r\n TreeNode *left = nullptr;\r\n TreeNode *right = nullptr;\r\n\r\n TreeNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nclass levelPair\r\n{\r\npublic:\r\n TreeNode *par = nullptr;\r\n int lb = -(int)1e8;\r\n int rb = (int)1e8;\r\n\r\n levelPair()\r\n {\r\n }\r\n\r\n levelPair(TreeNode *par, int lb, int rb)\r\n {\r\n this->par = par;\r\n this->lb = lb;\r\n this->rb = rb;\r\n }\r\n};\r\n\r\nTreeNode *constructBSTFromLevelOrder(vector<int> &arr)\r\n{\r\n return nullptr;\r\n}\r\n\r\n// input_Section_====================================================================\r\n\r\nvoid display(TreeNode *node)\r\n{\r\n if (node == nullptr)\r\n return;\r\n\r\n string str = \"\";\r\n str += ((node->left != nullptr ? to_string(node->left->val) : \".\"));\r\n str += (\" -> \" + to_string(node->val) + \" <- \");\r\n str += ((node->right != nullptr ? to_string(node->right->val) : \".\"));\r\n\r\n cout << str << endl;\r\n\r\n display(node->left);\r\n display(node->right);\r\n}\r\n\r\nvoid solve()\r\n{\r\n int n;\r\n cin >> n;\r\n vector<int> pre(n, 0);\r\n for (int i = 0; i < n; i++)\r\n {\r\n cin >> pre[i];\r\n }\r\n\r\n TreeNode *root = constructBSTFromLevelOrder(pre);\r\n display(root);\r\n}\r\n\r\nint main()\r\n{\r\n solve();\r\n return 0;\r\n}"},"java":{"code":"import java.util.Scanner;\r\n\r\npublic class Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class TreeNode {\r\n int val = 0;\r\n TreeNode left = null;\r\n TreeNode right = null;\r\n\r\n TreeNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static TreeNode constructBSTFromLevelOrder(int[] LevelOrder) {\r\n return null;\r\n }\r\n\r\n // input_section=================================================\r\n\r\n public static void display(TreeNode node) {\r\n if (node == null)\r\n return;\r\n\r\n StringBuilder sb = new StringBuilder();\r\n sb.append((node.left != null ? node.left.val : \".\"));\r\n sb.append(\" -> \" + node.val + \" <- \");\r\n sb.append((node.right != null ? node.right.val : \".\"));\r\n\r\n System.out.println(sb.toString());\r\n\r\n display(node.left);\r\n display(node.right);\r\n\r\n }\r\n\r\n public static void solve() {\r\n int n = scn.nextInt();\r\n int[] level = new int[n];\r\n for (int i = 0; i < n; i++)\r\n level[i] = scn.nextInt();\r\n\r\n TreeNode root = constructBSTFromLevelOrder(level);\r\n display(root);\r\n }\r\n\r\n public static void main(String[] args) {\r\n solve();\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"6\r\n3 2 5 1 6 7","sampleOutput":"2 -> 3 <- 5\r\n1 -> 2 <- .\r\n. -> 1 <- .\r\n. -> 5 <- 6\r\n. -> 6 <- 7\r\n. -> 7 <- .\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"2e9df04c-be14-441c-9a18-8b5a8ca6596d","name":"Trees For Intermediate","slug":"trees-for-intermediate-9994","type":0},{"id":"cd55418f-46a3-4df8-a904-614a73b595c6","name":"Construct Bst From Levelorder Traversal","slug":"construct-bst-from-levelorder-traversal","type":1}],"next":{"id":"be3e37ca-501e-48bc-afc1-aea3844f3540","name":"Construct Bst From Levelorder Traversal","type":3,"slug":"construct-bst-from-levelorder-traversal"},"prev":{"id":"389f5053-eca7-4f5b-9949-9c54ebe3d87c","name":"Right View Of A Binarytree","type":0,"slug":"right-view-of-a-binarytree"}}}` Editor

# Construct Bst From Levelorder Traversal

medium

1. You are given a partially written function to solve(Refer question video). 2. Task : Construct Binary Search Tree from given LevelOrder Traversal. 3. you will be given an array representing a valid LevelOrder of a Binary Search Tree. Program is required to create a unique Binary Search Tree.

## Constraints

0 <= Number of Nodes <= 10^9 -10^9 <= value of Node data <= 10^9 Valid LevelOrder traversal.

## Format

### Input

Input is managed for you.

### Output

Output is managed for you.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}6 3 2 5 1 6 7```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}2 -> 3 `

Discussions

Show Discussion

Related Resources 