`{"id":"ae56b32d-7e02-4408-80bf-c3235d1220c8","name":"Kth Largest Element In Tree","description":"1. You are given a partially written GenericTree class.\r\n2. You are given a number k. You are required to find and print the kth largest value in the tree.\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<iostream>\nusing namespace std;\nint c= INT_MAX;\nint flloor= INT_MIN;\n\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 //t->data=arr[i];\n \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\n\n\nvoid cnf(Node *node, int data){\n if(node->data > data)\n {\n if(node->data <c)\n {\n c = node->data;\n }\n }\n \n if(node->data < data)\n {\n if(node->data > flloor)\n {\n flloor = node->data;\n }\n }\n \n for(Node *child: node->children)\n {\n cnf(child,data);\n }\n \n}\n\nint kthlarge(Node *node, int k)\n{\n //Write your code here\n}\n\nint main(){\n \n int n;\n cin>>n;\n \n int arr[n];\n for(int i=0;i<n;i++){\n cin>>arr[i];\n }\n int k;\n cin>>k;\n Node *root=construct(arr,n);\n int kthlrg = kthlarge(root,k);\n cout<<kthlrg;\n}"},"java":{"code":"#include<bits/stdc++.h>\r\n#include<iostream>\r\nusing namespace std;\r\nint c= INT_MAX;\r\nint flloor= INT_MIN;\r\n\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 //t->data=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\n\r\n\r\nvoid cnf(Node *node, int data){\r\n if(node->data > data)\r\n {\r\n if(node->data <c)\r\n {\r\n c = node->data;\r\n }\r\n }\r\n \r\n if(node->data < data)\r\n {\r\n if(node->data > flloor)\r\n {\r\n flloor = node->data;\r\n }\r\n }\r\n \r\n for(Node *child: node->children)\r\n {\r\n cnf(child,data);\r\n }\r\n \r\n}\r\n\r\nint kthlarge(Node *node, int k)\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 int k;\r\n cin>>k;\r\n Node *root=construct(arr,n);\r\n int kthlrg = kthlarge(root,k);\r\n cout<<kthlrg;\r\n}"},"python":{"code":"import math\nceil=math.inf\n\nclass Node:\n \n def __init__(self, data):\n self.data = data\n self.child = []\n \n # Utility function to create a new tree node\ndef newNode(data): \n temp = Node(data)\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\n\ndef ceilndfloor(node, data):\n global floor\n global ceil\n if node.data > data:\n if node.data < ceil:\n ceil = node.data\n \n if node.data < data:\n if node.data > floor:\n floor = node.data\n \n for child in node.child:\n ceilndfloor(child,data)\n\ndef kthlargest(node,k):\n # Write your code here\n \n \n# number of elements as input\nn = int(input())\nlst = []\nlst = list(map(int, input().split()))\n \n\nroot = constructor(lst,n) \nk = int(input())\nkth = kthlargest(root, k)\nprint(kth)"}},"points":10,"difficulty":"medium","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\n8","sampleOutput":"10","questionVideo":"https://www.youtube.com/embed/7Vi6GkgtWpg","hints":[],"associated":[{"id":"0d3e69df-ddcb-4c04-b343-6f8852c6fded","name":"What is floor value?","slug":"what-is-floor-value","type":4},{"id":"1a90a031-ea09-463b-b90f-7015a6966fde","name":"How we are finding value?","slug":"how-we-are-finding-value","type":4},{"id":"9d785335-dec2-4019-8954-ec3e080b7696","name":"If ‘k’s is 3 then what value do we have to print?","slug":"if-k-s-is-3-then-what-value-do-we-have-to-print","type":4},{"id":"d211a6d1-f1b3-4c82-9c26-d5c2dfa2d529","name":"How many times do we iterate?","slug":"how-many-times-do-we-iterate","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":"91177c36-b332-444c-b229-4a20b27633cc","name":"Kth Largest Element In Tree","slug":"kth-largest-element-in-tree","type":1}],"next":{"id":"592f81a7-be47-44d9-8b1a-170dbe6a19fc","name":"Kth Largest Element In Tree","type":3,"slug":"kth-largest-element-in-tree"},"prev":{"id":"84de08b5-b097-4b10-8623-00278b571167","name":"Ceil and Floor in a Generic Tree","type":3,"slug":"ceil-and-floor-in-a-generic-tree"}}}`

# Kth Largest Element In Tree

1. You are given a partially written GenericTree class. 2. You are given a number k. You are required to find and print the kth largest value in the tree. 3. Input and Output is managed for you.

`{"id":"ae56b32d-7e02-4408-80bf-c3235d1220c8","name":"Kth Largest Element In Tree","description":"1. You are given a partially written GenericTree class.\r\n2. You are given a number k. You are required to find and print the kth largest value in the tree.\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<iostream>\nusing namespace std;\nint c= INT_MAX;\nint flloor= INT_MIN;\n\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 //t->data=arr[i];\n \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\n\n\nvoid cnf(Node *node, int data){\n if(node->data > data)\n {\n if(node->data <c)\n {\n c = node->data;\n }\n }\n \n if(node->data < data)\n {\n if(node->data > flloor)\n {\n flloor = node->data;\n }\n }\n \n for(Node *child: node->children)\n {\n cnf(child,data);\n }\n \n}\n\nint kthlarge(Node *node, int k)\n{\n //Write your code here\n}\n\nint main(){\n \n int n;\n cin>>n;\n \n int arr[n];\n for(int i=0;i<n;i++){\n cin>>arr[i];\n }\n int k;\n cin>>k;\n Node *root=construct(arr,n);\n int kthlrg = kthlarge(root,k);\n cout<<kthlrg;\n}"},"java":{"code":"#include<bits/stdc++.h>\r\n#include<iostream>\r\nusing namespace std;\r\nint c= INT_MAX;\r\nint flloor= INT_MIN;\r\n\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 //t->data=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\n\r\n\r\nvoid cnf(Node *node, int data){\r\n if(node->data > data)\r\n {\r\n if(node->data <c)\r\n {\r\n c = node->data;\r\n }\r\n }\r\n \r\n if(node->data < data)\r\n {\r\n if(node->data > flloor)\r\n {\r\n flloor = node->data;\r\n }\r\n }\r\n \r\n for(Node *child: node->children)\r\n {\r\n cnf(child,data);\r\n }\r\n \r\n}\r\n\r\nint kthlarge(Node *node, int k)\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 int k;\r\n cin>>k;\r\n Node *root=construct(arr,n);\r\n int kthlrg = kthlarge(root,k);\r\n cout<<kthlrg;\r\n}"},"python":{"code":"import math\nceil=math.inf\n\nclass Node:\n \n def __init__(self, data):\n self.data = data\n self.child = []\n \n # Utility function to create a new tree node\ndef newNode(data): \n temp = Node(data)\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\n\ndef ceilndfloor(node, data):\n global floor\n global ceil\n if node.data > data:\n if node.data < ceil:\n ceil = node.data\n \n if node.data < data:\n if node.data > floor:\n floor = node.data\n \n for child in node.child:\n ceilndfloor(child,data)\n\ndef kthlargest(node,k):\n # Write your code here\n \n \n# number of elements as input\nn = int(input())\nlst = []\nlst = list(map(int, input().split()))\n \n\nroot = constructor(lst,n) \nk = int(input())\nkth = kthlargest(root, k)\nprint(kth)"}},"points":10,"difficulty":"medium","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\n8","sampleOutput":"10","questionVideo":"https://www.youtube.com/embed/7Vi6GkgtWpg","hints":[],"associated":[{"id":"0d3e69df-ddcb-4c04-b343-6f8852c6fded","name":"What is floor value?","slug":"what-is-floor-value","type":4},{"id":"1a90a031-ea09-463b-b90f-7015a6966fde","name":"How we are finding value?","slug":"how-we-are-finding-value","type":4},{"id":"9d785335-dec2-4019-8954-ec3e080b7696","name":"If ‘k’s is 3 then what value do we have to print?","slug":"if-k-s-is-3-then-what-value-do-we-have-to-print","type":4},{"id":"d211a6d1-f1b3-4c82-9c26-d5c2dfa2d529","name":"How many times do we iterate?","slug":"how-many-times-do-we-iterate","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":"91177c36-b332-444c-b229-4a20b27633cc","name":"Kth Largest Element In Tree","slug":"kth-largest-element-in-tree","type":1}],"next":{"id":"592f81a7-be47-44d9-8b1a-170dbe6a19fc","name":"Kth Largest Element In Tree","type":3,"slug":"kth-largest-element-in-tree"},"prev":{"id":"84de08b5-b097-4b10-8623-00278b571167","name":"Ceil and Floor in a Generic Tree","type":3,"slug":"ceil-and-floor-in-a-generic-tree"}}}`

Editor

# Kth Largest Element In Tree

medium

1. You are given a partially written GenericTree class. 2. You are given a number k. You are required to find and print the kth largest value in the tree. 3. Input and Output is managed for you.

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;}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 8```

### 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;}10`

Question Video

Discussions

Show Discussion

Related Resources