`{"id":"94c733ab-527b-40ec-9979-0eca86d081ff","name":"Is Generic Tree Symmetric","description":"1. You are given a partially written GenericTree class.\r\n2. You are required to complete the body of IsSymmetric function. The function is expected to check if the tree is symmetric, if so return true otherwise return false. For knowing symmetricity think of face and hand. Face is symmetric while palm is not. Also, we are check only smmetricity of shape and not content. Check 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>\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 \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\nbool issym(Node *node)\r\n{\r\n //Write your code here\r\n}\r\n\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 Node *root=construct(arr,n);\r\n bool sym = issym(root);\r\n if(sym == 1){\r\n cout<<\"true\";\r\n }\r\n else\r\n {\r\n cout<<\"false\";\r\n }\r\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 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 IsSymmetric(Node node) {\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 Node root = construct(arr);\r\n boolean sym = IsSymmetric(root);\r\n System.out.println(sym);\r\n // display(root);\r\n }\r\n\r\n}"},"python":{"code":"class Node:\r\n \r\n def __init__(self, data):\r\n \r\n self.data = data\r\n self.child = []\r\n \r\n # Utility function to create a new tree node\r\ndef newNode(data): \r\n temp = Node(data)\r\n return temp\r\n \r\n# Prints the n-ary tree level wise\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\n\r\ndef issym(node):\r\n #Write your code here\r\n \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\nsym = issym(root)\r\nif sym == True:\r\n print(\"true\")\r\nelse:\r\n print(\"false\")"}},"points":10,"difficulty":"easy","sampleInput":"20\r\n10 20 50 -1 60 -1 -1 30 70 -1 80 -1 90 -1 -1 40 100 -1 110 -1 -1 -1","sampleOutput":"true","questionVideo":"https://www.youtube.com/embed/ewEAjK83ZVM","hints":[],"associated":[{"id":"1155f48a-b3ec-41c4-9aec-893b456316d5","name":"How to state if a Generic Tree is Symmetric or Not?","slug":"how-to-state-if-a-generic-tree-is-symmetric-or-not","type":4},{"id":"20497fb8-35cb-402f-88c5-db2c1ed4f9da","name":"What will be the overall Time Complexity of \"Is Generic Tree Symmetric \"?","slug":"what-will-be-the-overall-time-complexity-of-is-generic-tree-symmetric","type":4},{"id":"effd8ecd-c9a3-4c03-a9e7-0b81e12d8f71","name":"What is the Space Complexity of \"Is Generic Tree Symmetric \"?","slug":"what-is-the-space-complexity-of-is-generic-tree-symmetric","type":4},{"id":"f5a0b338-87b4-4941-8bf7-8ccc72217b44","name":"What should be the return type of areMirror() function ?","slug":"what-should-be-the-return-type-of-aremirror-function","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":"d5c7d1b4-d8a0-4504-a1b1-ef578ca36d74","name":"Is Generic Tree Symmetric","slug":"is-generic-tree-symmetric","type":1}],"next":{"id":"1b217e12-57a2-4c8d-9a97-94e57d94d5cf","name":"Is Generic tree Symmetric","type":3,"slug":"is-generic-tree-symmetric"},"prev":{"id":"468c2bed-6ddc-461e-b2f4-ab497ec12c03","name":"Are trees mirror in shape","type":3,"slug":"are-trees-mirror-in-shape"}}}`

# Is Generic Tree Symmetric

1. You are given a partially written GenericTree class. 2. You are required to complete the body of IsSymmetric function. The function is expected to check if the tree is symmetric, if so return true otherwise return false. For knowing symmetricity think of face and hand. Face is symmetric while palm is not. Also, we are check only smmetricity of shape and not content. Check the question video for clarity. 3. Input and Output is managed for you.

`{"id":"94c733ab-527b-40ec-9979-0eca86d081ff","name":"Is Generic Tree Symmetric","description":"1. You are given a partially written GenericTree class.\r\n2. You are required to complete the body of IsSymmetric function. The function is expected to check if the tree is symmetric, if so return true otherwise return false. For knowing symmetricity think of face and hand. Face is symmetric while palm is not. Also, we are check only smmetricity of shape and not content. Check 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>\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 \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\nbool issym(Node *node)\r\n{\r\n //Write your code here\r\n}\r\n\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 Node *root=construct(arr,n);\r\n bool sym = issym(root);\r\n if(sym == 1){\r\n cout<<\"true\";\r\n }\r\n else\r\n {\r\n cout<<\"false\";\r\n }\r\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 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 IsSymmetric(Node node) {\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 Node root = construct(arr);\r\n boolean sym = IsSymmetric(root);\r\n System.out.println(sym);\r\n // display(root);\r\n }\r\n\r\n}"},"python":{"code":"class Node:\r\n \r\n def __init__(self, data):\r\n \r\n self.data = data\r\n self.child = []\r\n \r\n # Utility function to create a new tree node\r\ndef newNode(data): \r\n temp = Node(data)\r\n return temp\r\n \r\n# Prints the n-ary tree level wise\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\n\r\ndef issym(node):\r\n #Write your code here\r\n \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\nsym = issym(root)\r\nif sym == True:\r\n print(\"true\")\r\nelse:\r\n print(\"false\")"}},"points":10,"difficulty":"easy","sampleInput":"20\r\n10 20 50 -1 60 -1 -1 30 70 -1 80 -1 90 -1 -1 40 100 -1 110 -1 -1 -1","sampleOutput":"true","questionVideo":"https://www.youtube.com/embed/ewEAjK83ZVM","hints":[],"associated":[{"id":"1155f48a-b3ec-41c4-9aec-893b456316d5","name":"How to state if a Generic Tree is Symmetric or Not?","slug":"how-to-state-if-a-generic-tree-is-symmetric-or-not","type":4},{"id":"20497fb8-35cb-402f-88c5-db2c1ed4f9da","name":"What will be the overall Time Complexity of \"Is Generic Tree Symmetric \"?","slug":"what-will-be-the-overall-time-complexity-of-is-generic-tree-symmetric","type":4},{"id":"effd8ecd-c9a3-4c03-a9e7-0b81e12d8f71","name":"What is the Space Complexity of \"Is Generic Tree Symmetric \"?","slug":"what-is-the-space-complexity-of-is-generic-tree-symmetric","type":4},{"id":"f5a0b338-87b4-4941-8bf7-8ccc72217b44","name":"What should be the return type of areMirror() function ?","slug":"what-should-be-the-return-type-of-aremirror-function","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":"d5c7d1b4-d8a0-4504-a1b1-ef578ca36d74","name":"Is Generic Tree Symmetric","slug":"is-generic-tree-symmetric","type":1}],"next":{"id":"1b217e12-57a2-4c8d-9a97-94e57d94d5cf","name":"Is Generic tree Symmetric","type":3,"slug":"is-generic-tree-symmetric"},"prev":{"id":"468c2bed-6ddc-461e-b2f4-ab497ec12c03","name":"Are trees mirror in shape","type":3,"slug":"are-trees-mirror-in-shape"}}}`

Editor

# Is Generic Tree Symmetric

easy

1. You are given a partially written GenericTree class. 2. You are required to complete the body of IsSymmetric function. The function is expected to check if the tree is symmetric, if so return true otherwise return false. For knowing symmetricity think of face and hand. Face is symmetric while palm is not. Also, we are check only smmetricity of shape and not content. Check 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;}20 10 20 50 -1 60 -1 -1 30 70 -1 80 -1 90 -1 -1 40 100 -1 110 -1 -1 -1```

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

Question Video

Discussions

Show Discussion

Related Resources