{"id":"30b0fba3-2aaf-45ee-9c4b-6bc66e5ecebb","name":"Recover Bst","description":"1. You are given a partially written function to solve(Refer question video).\r\n2. You are given the root of a binary search tree (BST), where exactly two nodes of the tree were swapped by mistake. Recover the tree without changing its structure.\r\n\r\nFollow up: You are Not Allowed To Use Extra Space(Example : Storing All Element In Arrays).\r\n","inputFormat":"Input is managed for you.","outputFormat":"Output is managed for you. ","constraints":"0 &lt;= Number of Nodes &lt;= 10^9\r\n-10^9 &lt;= value of Node data &lt;= 10^9\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\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\nvoid recoverTree(TreeNode *root)\r\n{\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\nTreeNode *createTree(vector<int> &arr, vector<int> &IDX)\r\n{\r\n if (IDX[0] > arr.size() || arr[IDX[0]] == -1)\r\n {\r\n IDX[0]++;\r\n return nullptr;\r\n }\r\n\r\n TreeNode *node = new TreeNode(arr[IDX[0]++]);\r\n node->left = createTree(arr, IDX);\r\n node->right = createTree(arr, IDX);\r\n\r\n return node;\r\n}\r\n\r\nvoid solve()\r\n{\r\n int n;\r\n cin >> n;\r\n vector<int> arr(n, 0);\r\n for (int i = 0; i < n; i++)\r\n {\r\n cin >> arr[i];\r\n }\r\n\r\n vector<int> IDX(1, 0);\r\n TreeNode *root = createTree(arr, IDX);\r\n recoverTree(root);\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 void recoverTree(TreeNode root) {\r\n\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 TreeNode createTree(int[] arr, int[] IDX) {\r\n if (IDX[0] > arr.length || arr[IDX[0]] == -1) {\r\n IDX[0]++;\r\n return null;\r\n }\r\n\r\n TreeNode node = new TreeNode(arr[IDX[0]++]);\r\n node.left = createTree(arr, IDX);\r\n node.right = createTree(arr, IDX);\r\n\r\n return node;\r\n }\r\n\r\n public static void solve() {\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for (int i = 0; i < n; i++)\r\n arr[i] = scn.nextInt();\r\n\r\n int[] IDX = new int[1];\r\n TreeNode root = createTree(arr, IDX);\r\n recoverTree(root);\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":"easy","sampleInput":"13\r\n7\r\n3\r\n2\r\n-1\r\n-1\r\n10\r\n-1\r\n-1\r\n5\r\n-1\r\n12\r\n-1\r\n-1","sampleOutput":"3 -> 7 <- 10\r\n2 -> 3 <- 5\r\n. -> 2 <- .\r\n. -> 5 <- .\r\n. -> 10 <- 12\r\n. -> 12 <- .\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":"3c71b909-3d31-4315-aab6-64d9ee46b695","name":"Recover Bst","slug":"recover-bst","type":1}],"next":{"id":"07ed2b6a-70b8-4cb3-89a6-3085f4a4716b","name":"Recover BST","type":3,"slug":"recover-bst"},"prev":{"id":"deecfc87-e938-4baf-8b50-6c86f0a2ca72","name":"Validate BST","type":3,"slug":"validate-bst"}}}

Recover Bst

1. You are given a partially written function to solve(Refer question video). 2. You are given the root of a binary search tree (BST), where exactly two nodes of the tree were swapped by mistake. Recover the tree without changing its structure. Follow up: You are Not Allowed To Use Extra Space(Example : Storing All Element In Arrays).

{"id":"30b0fba3-2aaf-45ee-9c4b-6bc66e5ecebb","name":"Recover Bst","description":"1. You are given a partially written function to solve(Refer question video).\r\n2. You are given the root of a binary search tree (BST), where exactly two nodes of the tree were swapped by mistake. Recover the tree without changing its structure.\r\n\r\nFollow up: You are Not Allowed To Use Extra Space(Example : Storing All Element In Arrays).\r\n","inputFormat":"Input is managed for you.","outputFormat":"Output is managed for you. ","constraints":"0 &lt;= Number of Nodes &lt;= 10^9\r\n-10^9 &lt;= value of Node data &lt;= 10^9\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\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\nvoid recoverTree(TreeNode *root)\r\n{\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\nTreeNode *createTree(vector<int> &arr, vector<int> &IDX)\r\n{\r\n if (IDX[0] > arr.size() || arr[IDX[0]] == -1)\r\n {\r\n IDX[0]++;\r\n return nullptr;\r\n }\r\n\r\n TreeNode *node = new TreeNode(arr[IDX[0]++]);\r\n node->left = createTree(arr, IDX);\r\n node->right = createTree(arr, IDX);\r\n\r\n return node;\r\n}\r\n\r\nvoid solve()\r\n{\r\n int n;\r\n cin >> n;\r\n vector<int> arr(n, 0);\r\n for (int i = 0; i < n; i++)\r\n {\r\n cin >> arr[i];\r\n }\r\n\r\n vector<int> IDX(1, 0);\r\n TreeNode *root = createTree(arr, IDX);\r\n recoverTree(root);\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 void recoverTree(TreeNode root) {\r\n\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 TreeNode createTree(int[] arr, int[] IDX) {\r\n if (IDX[0] > arr.length || arr[IDX[0]] == -1) {\r\n IDX[0]++;\r\n return null;\r\n }\r\n\r\n TreeNode node = new TreeNode(arr[IDX[0]++]);\r\n node.left = createTree(arr, IDX);\r\n node.right = createTree(arr, IDX);\r\n\r\n return node;\r\n }\r\n\r\n public static void solve() {\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for (int i = 0; i < n; i++)\r\n arr[i] = scn.nextInt();\r\n\r\n int[] IDX = new int[1];\r\n TreeNode root = createTree(arr, IDX);\r\n recoverTree(root);\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":"easy","sampleInput":"13\r\n7\r\n3\r\n2\r\n-1\r\n-1\r\n10\r\n-1\r\n-1\r\n5\r\n-1\r\n12\r\n-1\r\n-1","sampleOutput":"3 -> 7 <- 10\r\n2 -> 3 <- 5\r\n. -> 2 <- .\r\n. -> 5 <- .\r\n. -> 10 <- 12\r\n. -> 12 <- .\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":"3c71b909-3d31-4315-aab6-64d9ee46b695","name":"Recover Bst","slug":"recover-bst","type":1}],"next":{"id":"07ed2b6a-70b8-4cb3-89a6-3085f4a4716b","name":"Recover BST","type":3,"slug":"recover-bst"},"prev":{"id":"deecfc87-e938-4baf-8b50-6c86f0a2ca72","name":"Validate BST","type":3,"slug":"validate-bst"}}}
plane

Editor


Loading...

Recover Bst

easy

1. You are given a partially written function to solve(Refer question video). 2. You are given the root of a binary search tree (BST), where exactly two nodes of the tree were swapped by mistake. Recover the tree without changing its structure. Follow up: You are Not Allowed To Use Extra Space(Example : Storing All Element In Arrays).

Constraints

0 <= Number of Nodes <= 10^9 -10^9 <= value of Node data <= 10^9

Format

Input

Input is managed for you.

Output

Output is managed for you.

Example

Sample Input

13 7 3 2 -1 -1 10 -1 -1 5 -1 12 -1 -1

Sample Output

3 -> 7

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode