`{"id":"6ec7f276-6943-42be-9784-aa35e16b2790","name":"Print In Range","description":"1. You are given a partially written BST class.\r\n2. You are required to complete the body of pir function. \"pir\" function is expected to print all nodes between d1 and d2 (inclusive and in increasing order).\r\n3. Input and Output is managed for you. \r\n\r\nNote -> Please watch the question video for clarity.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n#include <stack>\n#include <string>\n\nusing namespace std;\n\nclass Node{\npublic:\n int data;\n Node* left = nullptr; \n Node* right = nullptr;\n Node (int data)\n {\n this->data=data;\n }\n};\n\nclass Pair{\n Node* node = nullptr;\n int state=0;\n\n Pair(Node* node, int state) {\n this->node = node;\n this->state = state;\n }\n};\n\nvoid pir(Node* node, int d1, int d2)\n{\n // write your code here\n}\n\nNode* construct(vector<int> & arr) {\n Node* root = new Node(arr);\n pair<Node*, int> p = {root, 1};\n\n stack<pair<Node*, int>> st;\n st.push(p);\n\n int idx = 1;\n while (!st.empty()) {\n if (st.top().second == 1) {\n st.top().second++;\n if (arr[idx] != -1) {\n st.top().first->left = new Node(arr[idx]);\n pair<Node*, int> lp = {st.top().first->left, 1};\n st.push(lp);\n }\n else {\n st.top().first->left = nullptr;\n }\n idx++;\n }\n else if (st.top().second == 2) {\n st.top().second++;\n if (arr[idx] != -1) {\n st.top().first->right = new Node(arr[idx]);\n pair<Node*, int> rp = {st.top().first->right, 1};\n st.push(rp);\n } else {\n st.top().first->right = nullptr;\n }\n idx++;\n }\n else {\n st.pop();\n }\n\n }\n return root;\n}\n\nint main() {\n int n;\n cin>>n;\n vector<int> arr(n,0);\n \n for(int i = 0; i < n; i++) {\n string tmp;\n cin>>tmp;\n if (tmp==\"n\") {\n arr[i] = -1;\n } else {\n arr[i] = stoi(tmp);\n }\n }\n\n int k1;\n cin>>k1;\n int k2;\n cin>>k2;\n\n Node* root = construct(arr);\n pir(root,k1,k2);\n }"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class Node {\r\n int data;\r\n Node left;\r\n Node right;\r\n\r\n Node(int data, Node left, Node right) {\r\n this.data = data;\r\n this.left = left;\r\n this.right = right;\r\n }\r\n }\r\n\r\n public static class Pair {\r\n Node node;\r\n int state;\r\n\r\n Pair(Node node, int state) {\r\n this.node = node;\r\n this.state = state;\r\n }\r\n }\r\n\r\n public static Node construct(Integer[] arr) {\r\n Node root = new Node(arr, null, null);\r\n Pair rtp = new Pair(root, 1);\r\n\r\n Stack<Pair> st = new Stack<>();\r\n st.push(rtp);\r\n\r\n int idx = 0;\r\n while (st.size() > 0) {\r\n Pair top = st.peek();\r\n if (top.state == 1) {\r\n idx++;\r\n if (arr[idx] != null) {\r\n top.node.left = new Node(arr[idx], null, null);\r\n Pair lp = new Pair(top.node.left, 1);\r\n st.push(lp);\r\n } else {\r\n top.node.left = null;\r\n }\r\n\r\n top.state++;\r\n } else if (top.state == 2) {\r\n idx++;\r\n if (arr[idx] != null) {\r\n top.node.right = new Node(arr[idx], null, null);\r\n Pair rp = new Pair(top.node.right, 1);\r\n st.push(rp);\r\n } else {\r\n top.node.right = null;\r\n }\r\n\r\n top.state++;\r\n } else {\r\n st.pop();\r\n }\r\n }\r\n\r\n return root;\r\n }\r\n\r\n public static void display(Node node) {\r\n if (node == null) {\r\n return;\r\n }\r\n\r\n String str = \"\";\r\n str += node.left == null ? \".\" : node.left.data + \"\";\r\n str += \" <- \" + node.data + \" -> \";\r\n str += node.right == null ? \".\" : node.right.data + \"\";\r\n System.out.println(str);\r\n\r\n display(node.left);\r\n display(node.right);\r\n }\r\n\r\n public static void pir(Node node, int d1, int d2) {\r\n // write your code here\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n int n = Integer.parseInt(br.readLine());\r\n Integer[] arr = new Integer[n];\r\n String[] values = br.readLine().split(\" \");\r\n for (int i = 0; i < n; i++) {\r\n if (values[i].equals(\"n\") == false) {\r\n arr[i] = Integer.parseInt(values[i]);\r\n } else {\r\n arr[i] = null;\r\n }\r\n }\r\n\r\n int d1 = Integer.parseInt(br.readLine());\r\n int d2 = Integer.parseInt(br.readLine());\r\n\r\n Node root = construct(arr);\r\n pir(root, d1, d2);\r\n }\r\n\r\n}"},"python":{"code":"class Node:\n def __init__(self, data,left,right):\n self.data = data\n self.left = left\n self.right = right\n \nclass Pair:\n def __init__(self,node,st):\n self.node=node;\n self.state=st;\n \n\ndef construct(arr):\n root=Node(arr,None,None)\n rtp=Pair(root,1);\n \n st=[]\n st.append(rtp);\n \n idx=0;\n while(len(st)>0):\n top=st[-1];\n if top.state==1:\n idx+=1\n if(arr[idx]!=None):\n top.node.left = Node(arr[idx], None, None);\n lp = Pair(top.node.left, 1);\n st.append(lp);\n else:\n top.node.left=None;\n top.state+=1\n elif top.state==2:\n idx+=1\n if(arr[idx]!=None):\n top.node.right = Node(arr[idx], None, None);\n rp = Pair(top.node.right, 1);\n st.append(rp);\n else:\n top.node.right=None;\n top.state+=1\n else:\n st.pop();\n return root;\n \n\ndef pir(node,d1, d2):\n #write your code here\n \nn=int(input())\nvalues = list(map(str, input().split()))\narr=*n\nfor i in range(0,n):\n if values[i]!=\"n\":\n arr[i]=int(values[i])\n else:\n arr[i]=None\nd1=int(input())\nd2=int(input())\nroot=construct(arr)\npir(root,d1,d2)"}},"points":10,"difficulty":"easy","sampleInput":"21\r\n50 25 12 n n 37 30 n n n 75 62 60 n n 70 n n 87 n n\r\n12\r\n65","sampleOutput":"12\r\n25\r\n30\r\n37\r\n50\r\n60\r\n62","questionVideo":"https://www.youtube.com/embed/PqXPLZS_yx4","hints":[],"associated":[{"id":"17e0cca6-e766-4a58-83ca-60ee2e4afd5f","name":"Which of the following is false about a binary search tree?","slug":"which-of-the-following-is-false-about-a-binary-search-tree","type":4},{"id":"40e6ca77-a1a4-4e9d-8101-aae01bac37d0","name":"The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree?","slug":"the-preorder-traversal-sequence-of-a-binary-search-tree-is-30-20-10-15-25-23-39-35-42-which-one-of-the-following-is-the-postorder-traversal-sequence-of-the-same-tree","type":4},{"id":"c4ee25d2-d067-4636-a337-9f0792e21176","name":"What are the worst case and average case complexities of Print In Range binary search tree?","slug":"what-are-the-worst-case-and-average-case-complexities-of-print-in-range-binary-search-tree","type":4}],"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":"af4e5bec-9225-4f5f-a99b-97b23e8cf431","name":"Binary Search Tree For Beginners","slug":"binary-search-tree-for-beginners","type":0},{"id":"d1874a4e-26a8-47a8-98cf-db54e82318b8","name":"Print In Range","slug":"print-in-range","type":1}],"next":{"id":"2d1ee10a-b7bb-4748-8594-4eeb5d456332","name":"Print In Range","type":3,"slug":"print-in-range"},"prev":{"id":"51d954e3-2a59-4a94-9754-9262ad919a56","name":"Lca Of Bst","type":3,"slug":"lca-of-bst"}}}`

# Print In Range

1. You are given a partially written BST class. 2. You are required to complete the body of pir function. "pir" function is expected to print all nodes between d1 and d2 (inclusive and in increasing order). 3. Input and Output is managed for you. Note -> Please watch the question video for clarity.

`{"id":"6ec7f276-6943-42be-9784-aa35e16b2790","name":"Print In Range","description":"1. You are given a partially written BST class.\r\n2. You are required to complete the body of pir function. \"pir\" function is expected to print all nodes between d1 and d2 (inclusive and in increasing order).\r\n3. Input and Output is managed for you. \r\n\r\nNote -> Please watch the question video for clarity.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n#include <stack>\n#include <string>\n\nusing namespace std;\n\nclass Node{\npublic:\n int data;\n Node* left = nullptr; \n Node* right = nullptr;\n Node (int data)\n {\n this->data=data;\n }\n};\n\nclass Pair{\n Node* node = nullptr;\n int state=0;\n\n Pair(Node* node, int state) {\n this->node = node;\n this->state = state;\n }\n};\n\nvoid pir(Node* node, int d1, int d2)\n{\n // write your code here\n}\n\nNode* construct(vector<int> & arr) {\n Node* root = new Node(arr);\n pair<Node*, int> p = {root, 1};\n\n stack<pair<Node*, int>> st;\n st.push(p);\n\n int idx = 1;\n while (!st.empty()) {\n if (st.top().second == 1) {\n st.top().second++;\n if (arr[idx] != -1) {\n st.top().first->left = new Node(arr[idx]);\n pair<Node*, int> lp = {st.top().first->left, 1};\n st.push(lp);\n }\n else {\n st.top().first->left = nullptr;\n }\n idx++;\n }\n else if (st.top().second == 2) {\n st.top().second++;\n if (arr[idx] != -1) {\n st.top().first->right = new Node(arr[idx]);\n pair<Node*, int> rp = {st.top().first->right, 1};\n st.push(rp);\n } else {\n st.top().first->right = nullptr;\n }\n idx++;\n }\n else {\n st.pop();\n }\n\n }\n return root;\n}\n\nint main() {\n int n;\n cin>>n;\n vector<int> arr(n,0);\n \n for(int i = 0; i < n; i++) {\n string tmp;\n cin>>tmp;\n if (tmp==\"n\") {\n arr[i] = -1;\n } else {\n arr[i] = stoi(tmp);\n }\n }\n\n int k1;\n cin>>k1;\n int k2;\n cin>>k2;\n\n Node* root = construct(arr);\n pir(root,k1,k2);\n }"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class Node {\r\n int data;\r\n Node left;\r\n Node right;\r\n\r\n Node(int data, Node left, Node right) {\r\n this.data = data;\r\n this.left = left;\r\n this.right = right;\r\n }\r\n }\r\n\r\n public static class Pair {\r\n Node node;\r\n int state;\r\n\r\n Pair(Node node, int state) {\r\n this.node = node;\r\n this.state = state;\r\n }\r\n }\r\n\r\n public static Node construct(Integer[] arr) {\r\n Node root = new Node(arr, null, null);\r\n Pair rtp = new Pair(root, 1);\r\n\r\n Stack<Pair> st = new Stack<>();\r\n st.push(rtp);\r\n\r\n int idx = 0;\r\n while (st.size() > 0) {\r\n Pair top = st.peek();\r\n if (top.state == 1) {\r\n idx++;\r\n if (arr[idx] != null) {\r\n top.node.left = new Node(arr[idx], null, null);\r\n Pair lp = new Pair(top.node.left, 1);\r\n st.push(lp);\r\n } else {\r\n top.node.left = null;\r\n }\r\n\r\n top.state++;\r\n } else if (top.state == 2) {\r\n idx++;\r\n if (arr[idx] != null) {\r\n top.node.right = new Node(arr[idx], null, null);\r\n Pair rp = new Pair(top.node.right, 1);\r\n st.push(rp);\r\n } else {\r\n top.node.right = null;\r\n }\r\n\r\n top.state++;\r\n } else {\r\n st.pop();\r\n }\r\n }\r\n\r\n return root;\r\n }\r\n\r\n public static void display(Node node) {\r\n if (node == null) {\r\n return;\r\n }\r\n\r\n String str = \"\";\r\n str += node.left == null ? \".\" : node.left.data + \"\";\r\n str += \" <- \" + node.data + \" -> \";\r\n str += node.right == null ? \".\" : node.right.data + \"\";\r\n System.out.println(str);\r\n\r\n display(node.left);\r\n display(node.right);\r\n }\r\n\r\n public static void pir(Node node, int d1, int d2) {\r\n // write your code here\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n int n = Integer.parseInt(br.readLine());\r\n Integer[] arr = new Integer[n];\r\n String[] values = br.readLine().split(\" \");\r\n for (int i = 0; i < n; i++) {\r\n if (values[i].equals(\"n\") == false) {\r\n arr[i] = Integer.parseInt(values[i]);\r\n } else {\r\n arr[i] = null;\r\n }\r\n }\r\n\r\n int d1 = Integer.parseInt(br.readLine());\r\n int d2 = Integer.parseInt(br.readLine());\r\n\r\n Node root = construct(arr);\r\n pir(root, d1, d2);\r\n }\r\n\r\n}"},"python":{"code":"class Node:\n def __init__(self, data,left,right):\n self.data = data\n self.left = left\n self.right = right\n \nclass Pair:\n def __init__(self,node,st):\n self.node=node;\n self.state=st;\n \n\ndef construct(arr):\n root=Node(arr,None,None)\n rtp=Pair(root,1);\n \n st=[]\n st.append(rtp);\n \n idx=0;\n while(len(st)>0):\n top=st[-1];\n if top.state==1:\n idx+=1\n if(arr[idx]!=None):\n top.node.left = Node(arr[idx], None, None);\n lp = Pair(top.node.left, 1);\n st.append(lp);\n else:\n top.node.left=None;\n top.state+=1\n elif top.state==2:\n idx+=1\n if(arr[idx]!=None):\n top.node.right = Node(arr[idx], None, None);\n rp = Pair(top.node.right, 1);\n st.append(rp);\n else:\n top.node.right=None;\n top.state+=1\n else:\n st.pop();\n return root;\n \n\ndef pir(node,d1, d2):\n #write your code here\n \nn=int(input())\nvalues = list(map(str, input().split()))\narr=*n\nfor i in range(0,n):\n if values[i]!=\"n\":\n arr[i]=int(values[i])\n else:\n arr[i]=None\nd1=int(input())\nd2=int(input())\nroot=construct(arr)\npir(root,d1,d2)"}},"points":10,"difficulty":"easy","sampleInput":"21\r\n50 25 12 n n 37 30 n n n 75 62 60 n n 70 n n 87 n n\r\n12\r\n65","sampleOutput":"12\r\n25\r\n30\r\n37\r\n50\r\n60\r\n62","questionVideo":"https://www.youtube.com/embed/PqXPLZS_yx4","hints":[],"associated":[{"id":"17e0cca6-e766-4a58-83ca-60ee2e4afd5f","name":"Which of the following is false about a binary search tree?","slug":"which-of-the-following-is-false-about-a-binary-search-tree","type":4},{"id":"40e6ca77-a1a4-4e9d-8101-aae01bac37d0","name":"The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree?","slug":"the-preorder-traversal-sequence-of-a-binary-search-tree-is-30-20-10-15-25-23-39-35-42-which-one-of-the-following-is-the-postorder-traversal-sequence-of-the-same-tree","type":4},{"id":"c4ee25d2-d067-4636-a337-9f0792e21176","name":"What are the worst case and average case complexities of Print In Range binary search tree?","slug":"what-are-the-worst-case-and-average-case-complexities-of-print-in-range-binary-search-tree","type":4}],"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":"af4e5bec-9225-4f5f-a99b-97b23e8cf431","name":"Binary Search Tree For Beginners","slug":"binary-search-tree-for-beginners","type":0},{"id":"d1874a4e-26a8-47a8-98cf-db54e82318b8","name":"Print In Range","slug":"print-in-range","type":1}],"next":{"id":"2d1ee10a-b7bb-4748-8594-4eeb5d456332","name":"Print In Range","type":3,"slug":"print-in-range"},"prev":{"id":"51d954e3-2a59-4a94-9754-9262ad919a56","name":"Lca Of Bst","type":3,"slug":"lca-of-bst"}}}` Editor

# Print In Range

easy

1. You are given a partially written BST class. 2. You are required to complete the body of pir function. "pir" function is expected to print all nodes between d1 and d2 (inclusive and in increasing order). 3. Input and Output is managed for you. Note -> Please watch the question video for clarity.

None

## 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;}21 50 25 12 n n 37 30 n n n 75 62 60 n n 70 n n 87 n n 12 65```

### 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;}12 25 30 37 50 60 62```

Question Video

Discussions

Show Discussion

Related Resources 