`{"id":"ec5ea0c2-e027-4d49-9526-4db28ee101a6","name":"Ceil And Floor In Generic Tree","description":"1. You are given a partially written GenericTree class.\r\n2. You are required to find the ceil and floor value of a given element. Use the \"travel and change\" strategy explained in the earlier video. The static properties - ceil and floor have been declared for you. You can declare more if you want. If the element is largest ceil will be largest integer value (32 bits), if element is smallest floor will be smallest integer value (32 bits). Watch the question video for clarity.\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;\n\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 \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\nint c= INT_MAX;\nint flloor= INT_MIN;\n\nvoid cnf(Node *node, int data){\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 data;\n cin>>data;\n Node *root=construct(arr,n);\n cnf(root,data);\n cout<<\"CEIL = \"<<c<<endl;\n cout<<\"FLOOR = \"<<flloor<<endl;\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 \r\n static int ceil;\r\n static int floor;\r\n public static void ceilAndFloor(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 ceil = Integer.MAX_VALUE;\r\n floor = Integer.MIN_VALUE;\r\n ceilAndFloor(root, data);\r\n System.out.println(\"CEIL = \" + ceil);\r\n System.out.println(\"FLOOR = \" + floor);\r\n }\r\n\r\n}"},"python":{"code":"import math\nfloor=-math.inf \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 #Write your code here\n\n\n \nlst = []\n# number of elements as input\nn = int(input())\n\nlst = list(map(int, input().split()))\n \n\nroot = constructor(lst,n) \ndata = int(input())\nceilndfloor(root, data)\n\nprint(\"CEIL = \" + str(ceil))\nprint(\"FLOOR = \"+ str(floor))"}},"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\n70","sampleOutput":"CEIL = 90\r\nFLOOR = 60","questionVideo":"https://www.youtube.com/embed/m__4qg_G_gs","hints":[],"associated":[{"id":"459c8ec4-99f2-48e1-b7ce-d4abcd6b30c6","name":"What do you mean by ceil of an element?","slug":"what-do-you-mean-by-ceil-of-an-element","type":4},{"id":"9d36ac6f-502f-4198-bd3c-e7f0234954c6","name":"What is the initial value for ceil and floor variable?","slug":"what-is-the-initial-value-for-ceil-and-floor-variable","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":"4f9498d9-c29b-4e9f-9193-fb3b8bfe8398","name":"Ceil And Floor In Generic Tree","slug":"ceil-and-floor-in-generic-tree","type":1}],"next":{"id":"84de08b5-b097-4b10-8623-00278b571167","name":"Ceil and Floor in a Generic Tree","type":3,"slug":"ceil-and-floor-in-a-generic-tree"},"prev":{"id":"eff5c874-de66-489f-930e-b6b4fab4e80f","name":"Predecessor and Successor of an Element","type":3,"slug":"predecessor-and-successor-of-an-element"}}}`

# Ceil And Floor In Generic Tree

1. You are given a partially written GenericTree class. 2. You are required to find the ceil and floor value of a given element. Use the "travel and change" strategy explained in the earlier video. The static properties - ceil and floor have been declared for you. You can declare more if you want. If the element is largest ceil will be largest integer value (32 bits), if element is smallest floor will be smallest integer value (32 bits). Watch the question video for clarity. 3. Input and Output is managed for you.

`{"id":"ec5ea0c2-e027-4d49-9526-4db28ee101a6","name":"Ceil And Floor In Generic Tree","description":"1. You are given a partially written GenericTree class.\r\n2. You are required to find the ceil and floor value of a given element. Use the \"travel and change\" strategy explained in the earlier video. The static properties - ceil and floor have been declared for you. You can declare more if you want. If the element is largest ceil will be largest integer value (32 bits), if element is smallest floor will be smallest integer value (32 bits). Watch the question video for clarity.\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;\n\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 \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\nint c= INT_MAX;\nint flloor= INT_MIN;\n\nvoid cnf(Node *node, int data){\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 data;\n cin>>data;\n Node *root=construct(arr,n);\n cnf(root,data);\n cout<<\"CEIL = \"<<c<<endl;\n cout<<\"FLOOR = \"<<flloor<<endl;\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 \r\n static int ceil;\r\n static int floor;\r\n public static void ceilAndFloor(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 ceil = Integer.MAX_VALUE;\r\n floor = Integer.MIN_VALUE;\r\n ceilAndFloor(root, data);\r\n System.out.println(\"CEIL = \" + ceil);\r\n System.out.println(\"FLOOR = \" + floor);\r\n }\r\n\r\n}"},"python":{"code":"import math\nfloor=-math.inf \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 #Write your code here\n\n\n \nlst = []\n# number of elements as input\nn = int(input())\n\nlst = list(map(int, input().split()))\n \n\nroot = constructor(lst,n) \ndata = int(input())\nceilndfloor(root, data)\n\nprint(\"CEIL = \" + str(ceil))\nprint(\"FLOOR = \"+ str(floor))"}},"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\n70","sampleOutput":"CEIL = 90\r\nFLOOR = 60","questionVideo":"https://www.youtube.com/embed/m__4qg_G_gs","hints":[],"associated":[{"id":"459c8ec4-99f2-48e1-b7ce-d4abcd6b30c6","name":"What do you mean by ceil of an element?","slug":"what-do-you-mean-by-ceil-of-an-element","type":4},{"id":"9d36ac6f-502f-4198-bd3c-e7f0234954c6","name":"What is the initial value for ceil and floor variable?","slug":"what-is-the-initial-value-for-ceil-and-floor-variable","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":"4f9498d9-c29b-4e9f-9193-fb3b8bfe8398","name":"Ceil And Floor In Generic Tree","slug":"ceil-and-floor-in-generic-tree","type":1}],"next":{"id":"84de08b5-b097-4b10-8623-00278b571167","name":"Ceil and Floor in a Generic Tree","type":3,"slug":"ceil-and-floor-in-a-generic-tree"},"prev":{"id":"eff5c874-de66-489f-930e-b6b4fab4e80f","name":"Predecessor and Successor of an Element","type":3,"slug":"predecessor-and-successor-of-an-element"}}}`

Editor

# Ceil And Floor In Generic Tree

easy

1. You are given a partially written GenericTree class. 2. You are required to find the ceil and floor value of a given element. Use the "travel and change" strategy explained in the earlier video. The static properties - ceil and floor have been declared for you. You can declare more if you want. If the element is largest ceil will be largest integer value (32 bits), if element is smallest floor will be smallest integer value (32 bits). Watch the question video for clarity. 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 70```

### 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;}CEIL = 90 FLOOR = 60```

Question Video

Discussions

Show Discussion

Related Resources