{"id":"700cbe55-c70c-4137-a278-66f3c6d4e301","name":"Maximum In A Generic Tree","description":"<p>1. You are given a partially written GenericTree class. 2. You are required to complete the body of max function. The function is expected to find the node with maximum value and return it. 3. Input and Output is managed for you.</p>","inputFormat":"<p>Input is managed for you</p>","outputFormat":"<p>Output is managed for you</p>","constraints":"<p>None</p>","sampleCode":{"cpp":{"code":"import math\r\nclass Node:\r\n \r\n def __init__(self, key):\r\n \r\n self.key = key\r\n self.child = []\r\n \r\ndef newNode(key): \r\n temp = Node(key)\r\n return temp\r\n\r\ndef constructor(lst,n):\r\n root = None\r\n stack = []\r\n for i in range(0,n):\r\n if lst[i]==-1:\r\n stack.pop()\r\n else:\r\n t= Node(lst[i])\r\n if len(stack) > 0:\r\n stack[-1].child.append(t)\r\n \r\n else:\r\n root=t\r\n \r\n stack.append(t)\r\n return root\r\n \r\ndef maximum(root):\r\n #write your code here\r\n \r\nlst = []\r\n# number of elements as input\r\nn = int(input())\r\nlst = list(map(int, input().split()))\r\nroot = constructor(lst,n) \r\nprint(maximum(root))"},"java":{"code":"#include <bits/stdc++.h>\r\nusing namespace std;\r\nstruct Node{\r\n int data;\r\n vector<Node*>children;\r\n};\r\n\r\nNode *newNode(int key){\r\n\tNode *temp=new Node;\r\n\ttemp->data=key;\r\n\treturn temp;\r\n\r\n}\r\n\r\nNode *construct(int arr[],int n ){\r\n Node *root=NULL;\r\n stack<Node*>st;\r\n for(int i=0;i<n;i++){\r\n if(arr[i]==-1){\r\n st.pop();\r\n }else{\r\n Node *t=newNode(arr[i]);\r\n \r\n if(st.size()>0){\r\n st.top()->children.push_back(t);\r\n }else{\r\n root=t;\r\n }\r\n st.push(t);\r\n }\r\n }\r\n return root;\r\n}\r\n\r\nint max1(Node *node)\r\n{\r\n //write your code here\r\n}\r\n\r\nint main(){\r\n \r\n int n;\r\n cin>>n;\r\n \r\n int arr[n];\r\n for(int i=0;i<n;i++){\r\n cin>>arr[i];\r\n }\r\n \r\n Node *root=construct(arr,n);\r\n int maximum=max1(root);\r\n \r\n cout << maximum << endl;\r\n}"},"python":{"code":"import math\nclass 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 maximum(root):\n #write your code here\n \nlst = []\n# number of elements as input\nn = int(input())\nlst = list(map(int, input().split()))\nroot = constructor(lst,n) \nprint(maximum(root))"}},"points":10,"difficulty":"easy","sampleInput":"12\r\n10 20 -1 30 50 -1 60 -1 -1 40 -1 -1","sampleOutput":"60","questionVideo":"https://www.youtube.com/embed/FqONnzlCSWQ","hints":[],"associated":[{"id":"0b89fb7f-c688-4017-a09e-5b06c0c7690e","name":"In which order, are we linearizing a Generic Tree?","slug":"in-which-order-are-we-linearizing-a-generic-tree","type":4},{"id":"130a7ff3-4ba9-4d8a-b80c-1548bcf9a26e","name":"What is the time complexity of approach to solve \"Maximum In A Generic Tree\"?","slug":"what-is-the-time-complexity-of-approach-to-solve-maximum-in-a-generic-tree","type":4},{"id":"217cd548-2050-4068-9a51-a6364f7c5bae","name":"What is the space complexity of approach to solve \"Maximum In A Generic Tree\"?","slug":"what-is-the-space-complexity-of-approach-to-solve-maximum-in-a-generic-tree","type":4},{"id":"3d6ea64f-cd29-479b-a33a-21703563db04","name":"Which of the following Data Structure is used in \"Maximum In A Generic Tree\"?","slug":"which-of-the-following-data-structure-is-used-in-maximum-in-a-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":"ac331e50-158e-4e51-85f2-cebeb98eb4ef","name":"Maximum In A Generic Tree","slug":"maximum-in-a-generic-tree","type":1}],"next":{"id":"73893be1-d1a8-4ca1-bef5-62976b607161","name":"Maximum in a Generic Tree","type":3,"slug":"maximum-in-a-generic-tree"},"prev":{"id":"5fb3915a-d830-4014-9a0b-8a161a0ae084","name":"Size of Generic Tree","type":3,"slug":"size-of-generic-tree"}}}

Maximum In A Generic Tree

<p>1. You are given a partially written GenericTree class. 2. You are required to complete the body of max function. The function is expected to find the node with maximum value and return it. 3. Input and Output is managed for you.</p>

{"id":"700cbe55-c70c-4137-a278-66f3c6d4e301","name":"Maximum In A Generic Tree","description":"<p>1. You are given a partially written GenericTree class. 2. You are required to complete the body of max function. The function is expected to find the node with maximum value and return it. 3. Input and Output is managed for you.</p>","inputFormat":"<p>Input is managed for you</p>","outputFormat":"<p>Output is managed for you</p>","constraints":"<p>None</p>","sampleCode":{"cpp":{"code":"import math\r\nclass Node:\r\n \r\n def __init__(self, key):\r\n \r\n self.key = key\r\n self.child = []\r\n \r\ndef newNode(key): \r\n temp = Node(key)\r\n return temp\r\n\r\ndef constructor(lst,n):\r\n root = None\r\n stack = []\r\n for i in range(0,n):\r\n if lst[i]==-1:\r\n stack.pop()\r\n else:\r\n t= Node(lst[i])\r\n if len(stack) > 0:\r\n stack[-1].child.append(t)\r\n \r\n else:\r\n root=t\r\n \r\n stack.append(t)\r\n return root\r\n \r\ndef maximum(root):\r\n #write your code here\r\n \r\nlst = []\r\n# number of elements as input\r\nn = int(input())\r\nlst = list(map(int, input().split()))\r\nroot = constructor(lst,n) \r\nprint(maximum(root))"},"java":{"code":"#include <bits/stdc++.h>\r\nusing namespace std;\r\nstruct Node{\r\n int data;\r\n vector<Node*>children;\r\n};\r\n\r\nNode *newNode(int key){\r\n\tNode *temp=new Node;\r\n\ttemp->data=key;\r\n\treturn temp;\r\n\r\n}\r\n\r\nNode *construct(int arr[],int n ){\r\n Node *root=NULL;\r\n stack<Node*>st;\r\n for(int i=0;i<n;i++){\r\n if(arr[i]==-1){\r\n st.pop();\r\n }else{\r\n Node *t=newNode(arr[i]);\r\n \r\n if(st.size()>0){\r\n st.top()->children.push_back(t);\r\n }else{\r\n root=t;\r\n }\r\n st.push(t);\r\n }\r\n }\r\n return root;\r\n}\r\n\r\nint max1(Node *node)\r\n{\r\n //write your code here\r\n}\r\n\r\nint main(){\r\n \r\n int n;\r\n cin>>n;\r\n \r\n int arr[n];\r\n for(int i=0;i<n;i++){\r\n cin>>arr[i];\r\n }\r\n \r\n Node *root=construct(arr,n);\r\n int maximum=max1(root);\r\n \r\n cout << maximum << endl;\r\n}"},"python":{"code":"import math\nclass 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 maximum(root):\n #write your code here\n \nlst = []\n# number of elements as input\nn = int(input())\nlst = list(map(int, input().split()))\nroot = constructor(lst,n) \nprint(maximum(root))"}},"points":10,"difficulty":"easy","sampleInput":"12\r\n10 20 -1 30 50 -1 60 -1 -1 40 -1 -1","sampleOutput":"60","questionVideo":"https://www.youtube.com/embed/FqONnzlCSWQ","hints":[],"associated":[{"id":"0b89fb7f-c688-4017-a09e-5b06c0c7690e","name":"In which order, are we linearizing a Generic Tree?","slug":"in-which-order-are-we-linearizing-a-generic-tree","type":4},{"id":"130a7ff3-4ba9-4d8a-b80c-1548bcf9a26e","name":"What is the time complexity of approach to solve \"Maximum In A Generic Tree\"?","slug":"what-is-the-time-complexity-of-approach-to-solve-maximum-in-a-generic-tree","type":4},{"id":"217cd548-2050-4068-9a51-a6364f7c5bae","name":"What is the space complexity of approach to solve \"Maximum In A Generic Tree\"?","slug":"what-is-the-space-complexity-of-approach-to-solve-maximum-in-a-generic-tree","type":4},{"id":"3d6ea64f-cd29-479b-a33a-21703563db04","name":"Which of the following Data Structure is used in \"Maximum In A Generic Tree\"?","slug":"which-of-the-following-data-structure-is-used-in-maximum-in-a-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":"ac331e50-158e-4e51-85f2-cebeb98eb4ef","name":"Maximum In A Generic Tree","slug":"maximum-in-a-generic-tree","type":1}],"next":{"id":"73893be1-d1a8-4ca1-bef5-62976b607161","name":"Maximum in a Generic Tree","type":3,"slug":"maximum-in-a-generic-tree"},"prev":{"id":"5fb3915a-d830-4014-9a0b-8a161a0ae084","name":"Size of Generic Tree","type":3,"slug":"size-of-generic-tree"}}}
plane

Editor


Loading...

Maximum In A Generic Tree

easy

1. You are given a partially written GenericTree class. 2. You are required to complete the body of max function. The function is expected to find the node with maximum value and return it. 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

12 10 20 -1 30 50 -1 60 -1 -1 40 -1 -1

Sample Output

60

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode