{"id":"0c4a4d6b-70fe-4ecf-b33e-548a1761003d","name":"Find In Generic Tree","description":"1. You are given a partially written GenericTree class.\r\n2. You are required to complete the body of find function. The function is expected to find the given data in the tree, if found it should return true or return false.\r\n3. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\n#include<climits>\n\nusing namespace std;\n\nclass Node\n{\npublic:\n int data = 0;\n vector<Node *> children;\n\n Node(int data)\n {\n this->data = data;\n }\n};\n\n\nvoid display(Node *node)\n{\n string s = \"\";\n s += to_string(node->data) + \" Child: \";\n for (Node *child : node->children)\n {\n s += to_string(child->data) + \", \";\n }\n\n cout << s << \".\" << endl;\n\n for (Node *child : node->children)\n {\n display(child);\n }\n}\n\n\nNode *constructor01(vector<int> &arr)\n{\n if (arr.size() == 0)\n return NULL;\n\n vector<Node *> stack;\n stack.push_back(new Node(arr[0]));\n\n Node *root = stack[0];\n\n for (int i = 1; i < arr.size(); i++)\n {\n if (arr[i] != -1)\n {\n Node *node = stack.back();\n Node *nnode = new Node(arr[i]);\n\n node->children.push_back(nnode);\n stack.push_back(nnode);\n }\n else\n stack.pop_back();\n }\n return root;\n}\n\n\nbool find(Node *node, int data)\n{\n // Write your code here\n}\n\n\n\nvoid solve()\n{\n int n;\n cin>>n;\n vector<int>arr(n,0);\n for(int i = 0; i < arr.size(); i++)\n {\n cin>>arr[i];\n }\n \n int data;\n cin>>data;\n \n Node *root = constructor01(arr);\n bool flag = find(root,data);\n if(flag == true){\n cout <<\"true\"<<endl;\n }\n else{\n cout <<\"false\"<<endl;\n }\n \n}\n\nint main()\n{\n solve();\n return 0;\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 boolean find(Node node, int data) {\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 int data = Integer.parseInt(br.readLine());\r\n\r\n Node root = construct(arr);\r\n boolean flag = find(root, data);\r\n System.out.println(flag);\r\n // display(root);\r\n }\r\n\r\n}"},"python":{"code":"class Node:\n def __init__(self, data):\n self.data = data\n self.children = []\n\n# create a new tree node\n\ndef newNode(data):\n temp = Node(data)\n return temp \n\n\n# n-ary tree level wise\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].children.append(t)\n\n else:\n root = t\n\n stack.append(t)\n\n return root\n\n\ndef find(root, data):\n \n # Write your code here\n \n \n\n# main function\n\nif __name__ == \"__main__\":\n \n lst = [] \n n = int(input())\n lst = list(map(int, input().split(\" \")))\n\n data = int(input())\n\n root = constructor(lst, n)\n\n flag = find(root,data)\n if flag == True:\n print(\"true\")\n \n else:\n print(\"false\")"}},"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\r\n120","sampleOutput":"true","questionVideo":"https://www.youtube.com/embed/H65rNN3TJkE","hints":[],"associated":[{"id":"09756241-f446-433c-9dcf-59eba42de4df","name":"What is the maximum number of children a generic tree node can have?","slug":"what-is-the-maximum-number-of-children-a-generic-tree-node-can-have","type":4},{"id":"7be74cba-23e9-41c7-ad63-e173715f21a4","name":"What will be the space-complexity for finding node_to_root_Path in generic tree?","slug":"what-will-be-the-space-complexity-for-finding-node-to-root-path-in-generic-tree","type":4},{"id":"9de0fb6c-67d0-4d3c-ad8e-e5c9164edd8f","name":"In which order are we working in this problem?","slug":"in-which-order-are-we-working-in-this-problem","type":4},{"id":"fae12bd1-c276-49ac-ab5e-36ce1a0a34e7","name":"What will be the time-complexity for finding an element in generic tree?","slug":"what-will-be-the-time-complexity-for-finding-an-element-in-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":"bb50ae6a-1d8f-45f9-9f84-e96d8a94435e","name":"Find In Generic Tree","slug":"find-in-generic-tree","type":1}],"next":{"id":"396290de-f968-439b-bdee-eb3895a8576b","name":"Find in Generic Tree","type":3,"slug":"find-in-generic-tree"},"prev":{"id":"d263c7ca-ba9e-4a0e-8553-f881b119de00","name":"Linearize A Generic Tree - Efficient Approach","type":0,"slug":"linearize-a-generic-tree-efficient-approach"}}}

Find In Generic Tree

1. You are given a partially written GenericTree class. 2. You are required to complete the body of find function. The function is expected to find the given data in the tree, if found it should return true or return false. 3. Input and Output is managed for you.

{"id":"0c4a4d6b-70fe-4ecf-b33e-548a1761003d","name":"Find In Generic Tree","description":"1. You are given a partially written GenericTree class.\r\n2. You are required to complete the body of find function. The function is expected to find the given data in the tree, if found it should return true or return false.\r\n3. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\n#include<climits>\n\nusing namespace std;\n\nclass Node\n{\npublic:\n int data = 0;\n vector<Node *> children;\n\n Node(int data)\n {\n this->data = data;\n }\n};\n\n\nvoid display(Node *node)\n{\n string s = \"\";\n s += to_string(node->data) + \" Child: \";\n for (Node *child : node->children)\n {\n s += to_string(child->data) + \", \";\n }\n\n cout << s << \".\" << endl;\n\n for (Node *child : node->children)\n {\n display(child);\n }\n}\n\n\nNode *constructor01(vector<int> &arr)\n{\n if (arr.size() == 0)\n return NULL;\n\n vector<Node *> stack;\n stack.push_back(new Node(arr[0]));\n\n Node *root = stack[0];\n\n for (int i = 1; i < arr.size(); i++)\n {\n if (arr[i] != -1)\n {\n Node *node = stack.back();\n Node *nnode = new Node(arr[i]);\n\n node->children.push_back(nnode);\n stack.push_back(nnode);\n }\n else\n stack.pop_back();\n }\n return root;\n}\n\n\nbool find(Node *node, int data)\n{\n // Write your code here\n}\n\n\n\nvoid solve()\n{\n int n;\n cin>>n;\n vector<int>arr(n,0);\n for(int i = 0; i < arr.size(); i++)\n {\n cin>>arr[i];\n }\n \n int data;\n cin>>data;\n \n Node *root = constructor01(arr);\n bool flag = find(root,data);\n if(flag == true){\n cout <<\"true\"<<endl;\n }\n else{\n cout <<\"false\"<<endl;\n }\n \n}\n\nint main()\n{\n solve();\n return 0;\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 boolean find(Node node, int data) {\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 int data = Integer.parseInt(br.readLine());\r\n\r\n Node root = construct(arr);\r\n boolean flag = find(root, data);\r\n System.out.println(flag);\r\n // display(root);\r\n }\r\n\r\n}"},"python":{"code":"class Node:\n def __init__(self, data):\n self.data = data\n self.children = []\n\n# create a new tree node\n\ndef newNode(data):\n temp = Node(data)\n return temp \n\n\n# n-ary tree level wise\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].children.append(t)\n\n else:\n root = t\n\n stack.append(t)\n\n return root\n\n\ndef find(root, data):\n \n # Write your code here\n \n \n\n# main function\n\nif __name__ == \"__main__\":\n \n lst = [] \n n = int(input())\n lst = list(map(int, input().split(\" \")))\n\n data = int(input())\n\n root = constructor(lst, n)\n\n flag = find(root,data)\n if flag == True:\n print(\"true\")\n \n else:\n print(\"false\")"}},"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\r\n120","sampleOutput":"true","questionVideo":"https://www.youtube.com/embed/H65rNN3TJkE","hints":[],"associated":[{"id":"09756241-f446-433c-9dcf-59eba42de4df","name":"What is the maximum number of children a generic tree node can have?","slug":"what-is-the-maximum-number-of-children-a-generic-tree-node-can-have","type":4},{"id":"7be74cba-23e9-41c7-ad63-e173715f21a4","name":"What will be the space-complexity for finding node_to_root_Path in generic tree?","slug":"what-will-be-the-space-complexity-for-finding-node-to-root-path-in-generic-tree","type":4},{"id":"9de0fb6c-67d0-4d3c-ad8e-e5c9164edd8f","name":"In which order are we working in this problem?","slug":"in-which-order-are-we-working-in-this-problem","type":4},{"id":"fae12bd1-c276-49ac-ab5e-36ce1a0a34e7","name":"What will be the time-complexity for finding an element in generic tree?","slug":"what-will-be-the-time-complexity-for-finding-an-element-in-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":"bb50ae6a-1d8f-45f9-9f84-e96d8a94435e","name":"Find In Generic Tree","slug":"find-in-generic-tree","type":1}],"next":{"id":"396290de-f968-439b-bdee-eb3895a8576b","name":"Find in Generic Tree","type":3,"slug":"find-in-generic-tree"},"prev":{"id":"d263c7ca-ba9e-4a0e-8553-f881b119de00","name":"Linearize A Generic Tree - Efficient Approach","type":0,"slug":"linearize-a-generic-tree-efficient-approach"}}}
plane

Editor


Loading...

Find In Generic Tree

easy

1. You are given a partially written GenericTree class. 2. You are required to complete the body of find function. The function is expected to find the given data in the tree, if found it should return true or return false. 3. Input and Output is managed for you.

Constraints

None

Format

Input

Input is managed for you

Output

Output is managed for you

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 120

Sample Output

true

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode