{"id":"e89eb8fd-9614-40dc-83e4-b751538e3f77","name":"Print K Levels Down","description":"1. You are given a partially written BinaryTree class.\r\n2. You are given a value k.\r\n3. You are required to complete the body of printKLevelsDown function. The function is expected to print in different lines all nodes which are k level deep. Use preorder for printing.\r\n4. Input is managed for you.","inputFormat":"Input is managed for you","outputFormat":"All nodes k-level deep printed in separated lines and visited in preorder","constraints":"None","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<string.h>\n#include<vector>\nusing namespace std;\nclass Node{\npublic:\n int data;\n Node* left = nullptr;\n Node* right = nullptr;\n Node(int data){\n this->data = data;\n }\n};\n\nclass Pair {\npublic:\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\nint idx = 0;\nNode* constructTree(vector<int>& arr){\n if (idx == arr.size() || arr[idx] == -1){\n idx++;\n return nullptr;\n }\n Node* node = new Node(arr[idx++]);\n node->left = constructTree(arr);\n node->right = constructTree(arr);\n return node;\n}\n\nvoid display(Node* node)\n{\n if (node == nullptr)\n return;\n string str = \"\";\n str += node->left != nullptr ? to_string(node->left->data) : \".\";\n str += \" <- \" + to_string(node->data) + \" -> \";\n str += node->right != nullptr ? to_string(node->right->data) : \".\";\n cout << str << endl;\n\n display(node->left);\n display(node->right);\n}\n\n\nvoid kLevelsDown(Node* node, int k){\n // write your code here\n}\n\nint main(){\n vector<int> arr;\n int n;\n cin >> n;\n for (int i = 0; i < n; i++) {\n string inp;\n cin >> inp;\n if (inp != \"n\") {\n arr.push_back(stoi(inp));\n }\n else {\n arr.push_back(-1);\n }\n }\n Node* root = constructTree(arr);\n int k;\n cin >> k;\n kLevelsDown(root, k);\n return 0;\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[0], 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 printKLevelsDown(Node node, int k){\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 k = Integer.parseInt(br.readLine());\r\n\r\n Node root = construct(arr);\r\n printKLevelsDown(root, k);\r\n }\r\n\r\n}"},"python":{"code":"import queue\nclass Node:\n \n def __init__(self,data,left,right):\n self.data = data\n self.left = None\n self.right = None\nclass Pair:\n def __init__(self,node,state):\n self.node = node\n self.state = state\n\ndef construct(arr):\n root=Node(arr[0],None,None)\n rtp=Pair(root,1);\n \n st=[]\n st.append(rtp);\n \n idx=0;\n n = len(arr)\n while(len(st)>0):\n top=st[-1];\n if top.state==1:\n idx+=1\n st[-1].state+=1\n if(arr[idx]!=-1):\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 elif top.state==2:\n idx+=1\n st[-1].state+=1\n if(arr[idx]!=-1):\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 else:\n st.pop()\n return root;\n \n\ndef printKLevelsDown(node, k):\n # write your code here\n \n \nn = int(input())\nst = input()\narr = [0]*n\narr = list(map(int,st.replace(\"n\",\"-1\").split(\" \")));\ndata = int(input())\nans = []\nroot = construct(arr)\nprintKLevelsDown(root, data)"}},"points":10,"difficulty":"easy","sampleInput":"19\r\n50 25 12 n n 37 30 n n n 75 62 n 70 n n 87 n n\r\n3","sampleOutput":"30\r\n70","questionVideo":"https://www.youtube.com/embed/DAY337wWbHY","hints":[],"associated":[{"id":"37f7ba44-505d-46d6-8ba5-d326291d89c5","name":"What are the number of arguments needed to be passed in the function?","slug":"what-are-the-number-of-arguments-needed-to-be-passed-in-the-function","type":4},{"id":"69ca3456-c36e-4d27-baf0-09722e44099d","name":"How many recursive calls are required in the solution?","slug":"how-many-recursive-calls-are-required-in-the-solution","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":"b042fe97-356f-41ed-80fd-abacd61fc234","name":"Binary Tree For Beginners","slug":"binary-tree-for-beginners","type":0},{"id":"af2ea808-4596-45a2-b08d-d2f29b2223d9","name":"Print K Levels Down","slug":"print-k-levels-down","type":1}],"next":{"id":"2f7310d1-f4c2-4e9d-ba7f-51a597f189e2","name":"Print K Levels Down","type":3,"slug":"print-k-levels-down"},"prev":{"id":"463aae3a-7085-424d-842e-93e939058dd1","name":"Find Node to Root Path in Binary Tree","type":3,"slug":"find-node-to-root-path-in-binary-tree"}}}

Print K Levels Down

1. You are given a partially written BinaryTree class. 2. You are given a value k. 3. You are required to complete the body of printKLevelsDown function. The function is expected to print in different lines all nodes which are k level deep. Use preorder for printing. 4. Input is managed for you.

{"id":"e89eb8fd-9614-40dc-83e4-b751538e3f77","name":"Print K Levels Down","description":"1. You are given a partially written BinaryTree class.\r\n2. You are given a value k.\r\n3. You are required to complete the body of printKLevelsDown function. The function is expected to print in different lines all nodes which are k level deep. Use preorder for printing.\r\n4. Input is managed for you.","inputFormat":"Input is managed for you","outputFormat":"All nodes k-level deep printed in separated lines and visited in preorder","constraints":"None","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<string.h>\n#include<vector>\nusing namespace std;\nclass Node{\npublic:\n int data;\n Node* left = nullptr;\n Node* right = nullptr;\n Node(int data){\n this->data = data;\n }\n};\n\nclass Pair {\npublic:\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\nint idx = 0;\nNode* constructTree(vector<int>& arr){\n if (idx == arr.size() || arr[idx] == -1){\n idx++;\n return nullptr;\n }\n Node* node = new Node(arr[idx++]);\n node->left = constructTree(arr);\n node->right = constructTree(arr);\n return node;\n}\n\nvoid display(Node* node)\n{\n if (node == nullptr)\n return;\n string str = \"\";\n str += node->left != nullptr ? to_string(node->left->data) : \".\";\n str += \" <- \" + to_string(node->data) + \" -> \";\n str += node->right != nullptr ? to_string(node->right->data) : \".\";\n cout << str << endl;\n\n display(node->left);\n display(node->right);\n}\n\n\nvoid kLevelsDown(Node* node, int k){\n // write your code here\n}\n\nint main(){\n vector<int> arr;\n int n;\n cin >> n;\n for (int i = 0; i < n; i++) {\n string inp;\n cin >> inp;\n if (inp != \"n\") {\n arr.push_back(stoi(inp));\n }\n else {\n arr.push_back(-1);\n }\n }\n Node* root = constructTree(arr);\n int k;\n cin >> k;\n kLevelsDown(root, k);\n return 0;\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[0], 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 printKLevelsDown(Node node, int k){\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 k = Integer.parseInt(br.readLine());\r\n\r\n Node root = construct(arr);\r\n printKLevelsDown(root, k);\r\n }\r\n\r\n}"},"python":{"code":"import queue\nclass Node:\n \n def __init__(self,data,left,right):\n self.data = data\n self.left = None\n self.right = None\nclass Pair:\n def __init__(self,node,state):\n self.node = node\n self.state = state\n\ndef construct(arr):\n root=Node(arr[0],None,None)\n rtp=Pair(root,1);\n \n st=[]\n st.append(rtp);\n \n idx=0;\n n = len(arr)\n while(len(st)>0):\n top=st[-1];\n if top.state==1:\n idx+=1\n st[-1].state+=1\n if(arr[idx]!=-1):\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 elif top.state==2:\n idx+=1\n st[-1].state+=1\n if(arr[idx]!=-1):\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 else:\n st.pop()\n return root;\n \n\ndef printKLevelsDown(node, k):\n # write your code here\n \n \nn = int(input())\nst = input()\narr = [0]*n\narr = list(map(int,st.replace(\"n\",\"-1\").split(\" \")));\ndata = int(input())\nans = []\nroot = construct(arr)\nprintKLevelsDown(root, data)"}},"points":10,"difficulty":"easy","sampleInput":"19\r\n50 25 12 n n 37 30 n n n 75 62 n 70 n n 87 n n\r\n3","sampleOutput":"30\r\n70","questionVideo":"https://www.youtube.com/embed/DAY337wWbHY","hints":[],"associated":[{"id":"37f7ba44-505d-46d6-8ba5-d326291d89c5","name":"What are the number of arguments needed to be passed in the function?","slug":"what-are-the-number-of-arguments-needed-to-be-passed-in-the-function","type":4},{"id":"69ca3456-c36e-4d27-baf0-09722e44099d","name":"How many recursive calls are required in the solution?","slug":"how-many-recursive-calls-are-required-in-the-solution","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":"b042fe97-356f-41ed-80fd-abacd61fc234","name":"Binary Tree For Beginners","slug":"binary-tree-for-beginners","type":0},{"id":"af2ea808-4596-45a2-b08d-d2f29b2223d9","name":"Print K Levels Down","slug":"print-k-levels-down","type":1}],"next":{"id":"2f7310d1-f4c2-4e9d-ba7f-51a597f189e2","name":"Print K Levels Down","type":3,"slug":"print-k-levels-down"},"prev":{"id":"463aae3a-7085-424d-842e-93e939058dd1","name":"Find Node to Root Path in Binary Tree","type":3,"slug":"find-node-to-root-path-in-binary-tree"}}}
plane

Editor


Loading...

Print K Levels Down

easy

1. You are given a partially written BinaryTree class. 2. You are given a value k. 3. You are required to complete the body of printKLevelsDown function. The function is expected to print in different lines all nodes which are k level deep. Use preorder for printing. 4. Input is managed for you.

Constraints

None

Format

Input

Input is managed for you

Output

All nodes k-level deep printed in separated lines and visited in preorder

Example

Sample Input

19 50 25 12 n n 37 30 n n n 75 62 n 70 n n 87 n n 3

Sample Output

30 70

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode