{"id":"0e32f9b7-b3b3-493a-a8e7-e051ed0468ad","name":"Level-order Of Generic Tree","description":"1. You are given a partially written GenericTree class.\r\n2. You are required to complete the body of levelorder function. The function is expected to visit every node in \"levelorder fashion\". Please check the question video for more details.\r\n3. Input is managed for you.","inputFormat":"Input is managed for you","outputFormat":"All nodes from left to right (level by level) all separated by a space and ending in a \".\"","constraints":"None","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\nstruct Node{\n int data;\n vector<Node*>children;\n};\n\nNode *newNode(int key){\n\tNode *temp=new Node;\n\ttemp->data=key;\n\treturn temp;\n\n}\n\nNode *construct(int arr[],int n ){\n Node *root=NULL;\n stack<Node*>st;\n for(int i=0;i<n;i++){\n if(arr[i]==-1){\n st.pop();\n }else{\n Node *t=newNode(arr[i]);\n if(st.size()>0){\n st.top()->children.push_back(t);\n }else{\n root=t;\n }\n st.push(t);\n }\n }\n return root;\n}\n\nvoid levelorder(Node *node)\n{\n //write your code here\n \n}\n\nint main(){\n int n;\n cin>>n;\n int arr[n];\n for(int i=0;i<n;i++){\n cin>>arr[i];\n }\n Node *root=construct(arr,n);\n levelorder(root);\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n private static class Node {\r\n int data;\r\n ArrayList<Node> children = new ArrayList<>();\r\n }\r\n\r\n public static void display(Node node) {\r\n String str = node.data + \" -> \";\r\n for (Node child : node.children) {\r\n str += child.data + \", \";\r\n }\r\n str += \".\";\r\n System.out.println(str);\r\n\r\n for (Node child : node.children) {\r\n display(child);\r\n }\r\n }\r\n\r\n public static Node construct(int[] arr) {\r\n Node root = null;\r\n\r\n Stack<Node> st = new Stack<>();\r\n for (int i = 0; i < arr.length; i++) {\r\n if (arr[i] == -1) {\r\n st.pop();\r\n } else {\r\n Node t = new Node();\r\n t.data = arr[i];\r\n\r\n if (st.size() > 0) {\r\n st.peek().children.add(t);\r\n } else {\r\n root = t;\r\n }\r\n\r\n st.push(t);\r\n }\r\n }\r\n\r\n return root;\r\n }\r\n\r\n public static int size(Node node) {\r\n int s = 0;\r\n\r\n for (Node child : node.children) {\r\n s += size(child);\r\n }\r\n s += 1;\r\n\r\n return s;\r\n }\r\n\r\n public static int max(Node node) {\r\n int m = Integer.MIN_VALUE;\r\n\r\n for (Node child : node.children) {\r\n int cm = max(child);\r\n m = Math.max(m, cm);\r\n }\r\n m = Math.max(m, node.data);\r\n\r\n return m;\r\n }\r\n\r\n public static int height(Node node) {\r\n int h = -1;\r\n\r\n for (Node child : node.children) {\r\n int ch = height(child);\r\n h = Math.max(h, ch);\r\n }\r\n h += 1;\r\n\r\n return h;\r\n }\r\n\r\n public static void traversals(Node node){\r\n System.out.println(\"Node Pre \" + node.data);\r\n\r\n for(Node child: node.children){\r\n System.out.println(\"Edge Pre \" + node.data + \"--\" + child.data);\r\n traversals(child);\r\n System.out.println(\"Edge Post \" + node.data + \"--\" + child.data);\r\n }\r\n\r\n System.out.println(\"Node Post \" + node.data);\r\n }\r\n\r\n public static void levelOrder(Node node){\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 int[] arr = new int[n];\r\n String[] values = br.readLine().split(\" \");\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = Integer.parseInt(values[i]);\r\n }\r\n\r\n Node root = construct(arr);\r\n levelOrder(root);\r\n }\r\n\r\n}"},"python":{"code":"class Node:\n \n def __init__(self, key):\n \n self.key = key\n self.child = []\n\ndef newNode(key): \n temp = Node(key)\n return temp\n\ndef constructor(lst,n):\n root = None\n stack = []\n for i in range(0,n):\n if lst[i]==-1:\n stack.pop()\n else:\n t= Node(lst[i])\n if len(stack) > 0:\n stack[-1].child.append(t)\n \n else:\n root=t\n \n stack.append(t)\n return root\n \ndef LevelOrderTraversal(root):\n #write your code here\n \nlst = []\nn = int(input())\nlst = list(map(int, input().split()))\nroot = constructor(lst,n) \nLevelOrderTraversal(root)"}},"points":10,"difficulty":"easy","sampleInput":"24\r\n10 20 50 -1 60 -1 -1 30 70 -1 80 110 -1 120 -1 -1 90 -1 -1 40 100 -1 -1 -1","sampleOutput":"10 20 30 40 50 60 70 80 90 100 110 120 .","questionVideo":"https://www.youtube.com/embed/ZHdnMxqgQOM","hints":[],"associated":[{"id":"6934a955-357d-45f2-bf7d-c395056ea64d","name":"What will be the data type of Queue in this question?","slug":"what-will-be-the-data-type-of-queue-in-this-question","type":4},{"id":"922a0ceb-535c-436f-aa47-714ac73fd65c","name":"Do we need a base case in \"Level-order of Generic tree\"?","slug":"do-we-need-a-base-case-in-level-order-of-generic-tree","type":4},{"id":"a4517e71-f445-4022-93b7-0a61c24d44c4","name":"What will be the sequence of the work that we have to do in \"Level-order of Generic tree\"?","slug":"what-will-be-the-sequence-of-the-work-that-we-have-to-do-in-level-order-of-generic-tree","type":4},{"id":"b7d7bf1d-5b90-4c8c-9519-a0edfb66dd1c","name":"Which of the following Data Structure is used in \"Level-order of Generic tree\"?","slug":"which-of-the-following-data-structure-is-used-in-level-order-of-generic-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":"b6a55c74-7dc3-45b6-a948-9dcbaebf9367","name":"Generic Tree For Beginners","slug":"generic-tree-for-beginners","type":0},{"id":"5e12ae3e-df2b-46c7-983d-a9279a0b0098","name":"Level-order Of Generic Tree","slug":"level-order-of-generic-tree","type":1}],"next":{"id":"7524f7aa-6534-4a0c-9031-394d6bec8f70","name":"Level Order of Generic Tree","type":3,"slug":"level-order-of-generic-tree"},"prev":{"id":"b2a624bc-33cd-4250-ae7d-91aeb7bf5eb1","name":"Traversals In Generic Tree","type":3,"slug":"traversals-in-generic-tree"}}}

Level-order Of Generic Tree

1. You are given a partially written GenericTree class. 2. You are required to complete the body of levelorder function. The function is expected to visit every node in "levelorder fashion". Please check the question video for more details. 3. Input is managed for you.

{"id":"0e32f9b7-b3b3-493a-a8e7-e051ed0468ad","name":"Level-order Of Generic Tree","description":"1. You are given a partially written GenericTree class.\r\n2. You are required to complete the body of levelorder function. The function is expected to visit every node in \"levelorder fashion\". Please check the question video for more details.\r\n3. Input is managed for you.","inputFormat":"Input is managed for you","outputFormat":"All nodes from left to right (level by level) all separated by a space and ending in a \".\"","constraints":"None","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\nstruct Node{\n int data;\n vector<Node*>children;\n};\n\nNode *newNode(int key){\n\tNode *temp=new Node;\n\ttemp->data=key;\n\treturn temp;\n\n}\n\nNode *construct(int arr[],int n ){\n Node *root=NULL;\n stack<Node*>st;\n for(int i=0;i<n;i++){\n if(arr[i]==-1){\n st.pop();\n }else{\n Node *t=newNode(arr[i]);\n if(st.size()>0){\n st.top()->children.push_back(t);\n }else{\n root=t;\n }\n st.push(t);\n }\n }\n return root;\n}\n\nvoid levelorder(Node *node)\n{\n //write your code here\n \n}\n\nint main(){\n int n;\n cin>>n;\n int arr[n];\n for(int i=0;i<n;i++){\n cin>>arr[i];\n }\n Node *root=construct(arr,n);\n levelorder(root);\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n private static class Node {\r\n int data;\r\n ArrayList<Node> children = new ArrayList<>();\r\n }\r\n\r\n public static void display(Node node) {\r\n String str = node.data + \" -> \";\r\n for (Node child : node.children) {\r\n str += child.data + \", \";\r\n }\r\n str += \".\";\r\n System.out.println(str);\r\n\r\n for (Node child : node.children) {\r\n display(child);\r\n }\r\n }\r\n\r\n public static Node construct(int[] arr) {\r\n Node root = null;\r\n\r\n Stack<Node> st = new Stack<>();\r\n for (int i = 0; i < arr.length; i++) {\r\n if (arr[i] == -1) {\r\n st.pop();\r\n } else {\r\n Node t = new Node();\r\n t.data = arr[i];\r\n\r\n if (st.size() > 0) {\r\n st.peek().children.add(t);\r\n } else {\r\n root = t;\r\n }\r\n\r\n st.push(t);\r\n }\r\n }\r\n\r\n return root;\r\n }\r\n\r\n public static int size(Node node) {\r\n int s = 0;\r\n\r\n for (Node child : node.children) {\r\n s += size(child);\r\n }\r\n s += 1;\r\n\r\n return s;\r\n }\r\n\r\n public static int max(Node node) {\r\n int m = Integer.MIN_VALUE;\r\n\r\n for (Node child : node.children) {\r\n int cm = max(child);\r\n m = Math.max(m, cm);\r\n }\r\n m = Math.max(m, node.data);\r\n\r\n return m;\r\n }\r\n\r\n public static int height(Node node) {\r\n int h = -1;\r\n\r\n for (Node child : node.children) {\r\n int ch = height(child);\r\n h = Math.max(h, ch);\r\n }\r\n h += 1;\r\n\r\n return h;\r\n }\r\n\r\n public static void traversals(Node node){\r\n System.out.println(\"Node Pre \" + node.data);\r\n\r\n for(Node child: node.children){\r\n System.out.println(\"Edge Pre \" + node.data + \"--\" + child.data);\r\n traversals(child);\r\n System.out.println(\"Edge Post \" + node.data + \"--\" + child.data);\r\n }\r\n\r\n System.out.println(\"Node Post \" + node.data);\r\n }\r\n\r\n public static void levelOrder(Node node){\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 int[] arr = new int[n];\r\n String[] values = br.readLine().split(\" \");\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = Integer.parseInt(values[i]);\r\n }\r\n\r\n Node root = construct(arr);\r\n levelOrder(root);\r\n }\r\n\r\n}"},"python":{"code":"class Node:\n \n def __init__(self, key):\n \n self.key = key\n self.child = []\n\ndef newNode(key): \n temp = Node(key)\n return temp\n\ndef constructor(lst,n):\n root = None\n stack = []\n for i in range(0,n):\n if lst[i]==-1:\n stack.pop()\n else:\n t= Node(lst[i])\n if len(stack) > 0:\n stack[-1].child.append(t)\n \n else:\n root=t\n \n stack.append(t)\n return root\n \ndef LevelOrderTraversal(root):\n #write your code here\n \nlst = []\nn = int(input())\nlst = list(map(int, input().split()))\nroot = constructor(lst,n) \nLevelOrderTraversal(root)"}},"points":10,"difficulty":"easy","sampleInput":"24\r\n10 20 50 -1 60 -1 -1 30 70 -1 80 110 -1 120 -1 -1 90 -1 -1 40 100 -1 -1 -1","sampleOutput":"10 20 30 40 50 60 70 80 90 100 110 120 .","questionVideo":"https://www.youtube.com/embed/ZHdnMxqgQOM","hints":[],"associated":[{"id":"6934a955-357d-45f2-bf7d-c395056ea64d","name":"What will be the data type of Queue in this question?","slug":"what-will-be-the-data-type-of-queue-in-this-question","type":4},{"id":"922a0ceb-535c-436f-aa47-714ac73fd65c","name":"Do we need a base case in \"Level-order of Generic tree\"?","slug":"do-we-need-a-base-case-in-level-order-of-generic-tree","type":4},{"id":"a4517e71-f445-4022-93b7-0a61c24d44c4","name":"What will be the sequence of the work that we have to do in \"Level-order of Generic tree\"?","slug":"what-will-be-the-sequence-of-the-work-that-we-have-to-do-in-level-order-of-generic-tree","type":4},{"id":"b7d7bf1d-5b90-4c8c-9519-a0edfb66dd1c","name":"Which of the following Data Structure is used in \"Level-order of Generic tree\"?","slug":"which-of-the-following-data-structure-is-used-in-level-order-of-generic-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":"b6a55c74-7dc3-45b6-a948-9dcbaebf9367","name":"Generic Tree For Beginners","slug":"generic-tree-for-beginners","type":0},{"id":"5e12ae3e-df2b-46c7-983d-a9279a0b0098","name":"Level-order Of Generic Tree","slug":"level-order-of-generic-tree","type":1}],"next":{"id":"7524f7aa-6534-4a0c-9031-394d6bec8f70","name":"Level Order of Generic Tree","type":3,"slug":"level-order-of-generic-tree"},"prev":{"id":"b2a624bc-33cd-4250-ae7d-91aeb7bf5eb1","name":"Traversals In Generic Tree","type":3,"slug":"traversals-in-generic-tree"}}}
plane

Editor


Loading...

Level-order Of Generic Tree

easy

1. You are given a partially written GenericTree class. 2. You are required to complete the body of levelorder function. The function is expected to visit every node in "levelorder fashion". Please check the question video for more details. 3. Input is managed for you.

Constraints

None

Format

Input

Input is managed for you

Output

All nodes from left to right (level by level) all separated by a space and ending in a "."

Example

Sample Input

24 10 20 50 -1 60 -1 -1 30 70 -1 80 110 -1 120 -1 -1 90 -1 -1 40 100 -1 -1 -1

Sample Output

10 20 30 40 50 60 70 80 90 100 110 120 .

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode