{"id":"65f30e41-7da8-4b24-9768-629c347c39a3","name":"Are Trees Mirror In Shape","description":"1. You are given a partially written GenericTree class.\r\n2. You are required to complete the body of areMirror function. The function is expected to check if the two trees passed to it are mirror images of each other in shape (data not to be checked, just the shape of tree).\r\nNote -> 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":""},"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 boolean areMirror(Node n1, Node n2) {\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\r\n int n1 = Integer.parseInt(br.readLine());\r\n int[] arr1 = new int[n1];\r\n String[] values1 = br.readLine().split(\" \");\r\n for (int i = 0; i < n1; i++) {\r\n arr1[i] = Integer.parseInt(values1[i]);\r\n }\r\n Node root1 = construct(arr1);\r\n\r\n int n2 = Integer.parseInt(br.readLine());\r\n int[] arr2 = new int[n2];\r\n String[] values2 = br.readLine().split(\" \");\r\n for (int i = 0; i < n2; i++) {\r\n arr2[i] = Integer.parseInt(values2[i]);\r\n }\r\n Node root2 = construct(arr2);\r\n\r\n boolean mirror = areMirror(root1, root2);\r\n System.out.println(mirror);\r\n }\r\n\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"12\r\n10 20 -1 30 50 -1 60 -1 -1 40 -1 -1\r\n12\r\n100 200 -1 300 500 -1 600 -1 -1 400 -1 -1","sampleOutput":"true\r\n","questionVideo":"https://www.youtube.com/embed/GfEQs3qhMws","hints":[],"associated":[],"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":"381d2c52-14b0-4c96-8604-2bbea6e7eec3","name":"Are Trees Mirror In Shape","slug":"are-trees-mirror-in-shape","type":1}],"next":{"id":"468c2bed-6ddc-461e-b2f4-ab497ec12c03","name":"Are trees mirror in shape","type":3,"slug":"are-trees-mirror-in-shape"},"prev":{"id":"5d698373-59e6-439c-8cb7-b7f45476edc8","name":"Are Trees Similar In Shape","type":3,"slug":"are-trees-similar-in-shape"}}}

Are Trees Mirror In Shape

1. You are given a partially written GenericTree class. 2. You are required to complete the body of areMirror function. The function is expected to check if the two trees passed to it are mirror images of each other in shape (data not to be checked, just the shape of tree). Note -> Watch the question video for clarity. 3. Input and Output is managed for you.

{"id":"65f30e41-7da8-4b24-9768-629c347c39a3","name":"Are Trees Mirror In Shape","description":"1. You are given a partially written GenericTree class.\r\n2. You are required to complete the body of areMirror function. The function is expected to check if the two trees passed to it are mirror images of each other in shape (data not to be checked, just the shape of tree).\r\nNote -> 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":""},"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 boolean areMirror(Node n1, Node n2) {\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\r\n int n1 = Integer.parseInt(br.readLine());\r\n int[] arr1 = new int[n1];\r\n String[] values1 = br.readLine().split(\" \");\r\n for (int i = 0; i < n1; i++) {\r\n arr1[i] = Integer.parseInt(values1[i]);\r\n }\r\n Node root1 = construct(arr1);\r\n\r\n int n2 = Integer.parseInt(br.readLine());\r\n int[] arr2 = new int[n2];\r\n String[] values2 = br.readLine().split(\" \");\r\n for (int i = 0; i < n2; i++) {\r\n arr2[i] = Integer.parseInt(values2[i]);\r\n }\r\n Node root2 = construct(arr2);\r\n\r\n boolean mirror = areMirror(root1, root2);\r\n System.out.println(mirror);\r\n }\r\n\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"12\r\n10 20 -1 30 50 -1 60 -1 -1 40 -1 -1\r\n12\r\n100 200 -1 300 500 -1 600 -1 -1 400 -1 -1","sampleOutput":"true\r\n","questionVideo":"https://www.youtube.com/embed/GfEQs3qhMws","hints":[],"associated":[],"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":"381d2c52-14b0-4c96-8604-2bbea6e7eec3","name":"Are Trees Mirror In Shape","slug":"are-trees-mirror-in-shape","type":1}],"next":{"id":"468c2bed-6ddc-461e-b2f4-ab497ec12c03","name":"Are trees mirror in shape","type":3,"slug":"are-trees-mirror-in-shape"},"prev":{"id":"5d698373-59e6-439c-8cb7-b7f45476edc8","name":"Are Trees Similar In Shape","type":3,"slug":"are-trees-similar-in-shape"}}}
plane

Editor


Loading...

Are Trees Mirror In Shape

easy

1. You are given a partially written GenericTree class. 2. You are required to complete the body of areMirror function. The function is expected to check if the two trees passed to it are mirror images of each other in shape (data not to be checked, just the shape of tree). Note -> Watch the question video for clarity. 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 12 100 200 -1 300 500 -1 600 -1 -1 400 -1 -1

Sample Output

true

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode