use `if (!root1.data.equals(root2.data))` instead of `if (!root1.data ==root2.data)) {`
because treenode contains Integer object not primitive int. so equals operator fails for >127 values with Integer objects
Problem of the day
You are given a binary tree, where the data present in each node is an integer. You have to find whether the given tree is symmetric or not.
Symmetric tree is a binary tree, whose mirror image is exactly the same as the original tree.
For Example:The only line of input contains the binary tree node elements in the level order form. The values of nodes are separated by a single space in a single line. In case a node is null, we take -1 in its place.
For example, the input for the tree depicted in the below image would be :
1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
Explanation :
Level 1 :
The root node of the tree is 1
Level 2 :
Left child of 1 = 2
Right child of 1 = 3
Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6
Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)
Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)
The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null (-1).
Note :
The above format was just to provide clarity on how the input is formed for a given tree.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output format:
The output consists of a single line containing "Symmetric" if the given tree is symmetric, else "Asymmetric".
Note:
You are not required to print the expected output; it has already been taken care of, Just implement the function.
0 <= N <= 10^5
1 <= Data <= 10^5
Where 'N' denotes the number of nodes in the given binary tree and 'Data' denotes the node value.
Time limit: 1sec
1 2 2 3 4 4 3 -1 -1 -1 -1 -1 -1 -1 -1
Symmetric
This is a symmetric tree:
1 2 3 4 -1 -1 -1 -1 -1
Asymmetric
This is an asymmetric tree:
Think about how you can obtain a mirror image of the tree. Does it have something to do with how you traverse the tree? More specifically, in case of a binary tree, which child do you traverse first?
We do two inorder traversals recursively of the tree simultaneously. For the first traversal, we traverse the left child first and then the right child, and in the second traversal, we traverse the right child first and then the left child. Let cur1 and cur2 be the current nodes of the first and second traversals respectively and initially both of them are passed as root. Then the algorithm follows:
O(N), Where N is the number of nodes present in the tree
Since we traverse every node once, the time complexity is O(N).
O(H), where H is the height of the tree (For a balanced tree, H = log(N))
This is because the recursion stack has a maximum depth of the height of our tree.
Interview problems
if you're java and getting 1 failed test case
use `if (!root1.data.equals(root2.data))` instead of `if (!root1.data ==root2.data)) {`
because treenode contains Integer object not primitive int. so equals operator fails for >127 values with Integer objects
Interview problems
easy to understand ,beats 99.6%solutions in c++
bool symmetric(BinaryTreeNode<int>*root1,BinaryTreeNode<int>* root2){
if(root1==nullptr && root2==nullptr){
return true;
}
if(root1==nullptr && root2!=nullptr){
return false;
}
if(root2==nullptr && root1!=nullptr){
return false;
}
if(root1->data!=root2->data){
return false;
}
bool a=symmetric(root1->left,root2->right);
bool b=symmetric(root1->right,root2->left);
return a==b;
}
bool isSymmetric(BinaryTreeNode<int>* root)
{
if(root==nullptr){
return true;
}
return symmetric(root->left,root->right);
}
Interview problems
why your code is failing on one testcase (root is null) here's the solution
Just return true if the root is NULL
bool isSameTree(BinaryTreeNode<int>* p, BinaryTreeNode<int>* q) {
if (p == NULL || q == NULL) {
return p == q;
}
return (p->data == q->data) && isSameTree(p->left, q->right) && isSameTree(p->right, q->left);
}
bool isSymmetric(BinaryTreeNode<int>* root) {
if (root == NULL) {
return true;
}
return isSameTree(root->left, root->right);
}
Interview problems
Symmetric Tree Easy CPP Solution 100 %
void left(BinaryTreeNode<int>* root,vector<int>& ans1)
{
if(root == NULL)
return ;
ans1.push_back(root->data);
left(root->left,ans1);
left(root->right,ans1);
}
void right(BinaryTreeNode<int>* root,vector<int>& ans2)
{
if(root == NULL)
return ;
ans2.push_back(root->data);
right(root->right,ans2);
right(root->left,ans2);
}
bool isSymmetric(BinaryTreeNode<int>* root)
{
vector<int> ans1,ans2;
left(root,ans1);
right(root,ans2);
if(ans1 == ans2)
return true;
return false;
}
Interview problems
Easy solution with c++ | Inorder traversal
void left(BinaryTreeNode<int>* root,vector<int>& ans1)
{
if(root == NULL)
return ;
ans1.push_back(root->data);
left(root->left,ans1);
left(root->right,ans1);
}
void right(BinaryTreeNode<int>* root,vector<int>& ans2)
{
if(root == NULL)
return ;
ans2.push_back(root->data);
right(root->right,ans2);
right(root->left,ans2);
}
bool isSymmetric(BinaryTreeNode<int>* root)
{
vector<int> ans1,ans2;
left(root,ans1);
right(root,ans2);
if(ans1 == ans2)
return true;
return false;
}
Interview problems
Java Code | Handles all the cases successfully| Easy to understand
public class Solution {
private static boolean isSymmetricUtil(BinaryTreeNode<Integer> root1, BinaryTreeNode<Integer> root2) {
if (root1 == null && root2 == null) {
return true;
}
if (root1 == null || root2 == null) {
return false;
}
if (!root1.data.equals(root2.data)) {
return false;
}
else return isSymmetricUtil(root1.left,root2.right) && isSymmetricUtil(root1.right,root2.left);
}
public static boolean isSymmetric(BinaryTreeNode<Integer> root) {
if(root==null)return true;
return isSymmetricUtil(root.left,root.right);
}
}
Interview problems
Why you are failing in one test case even though the same code works on LeetCode (EXPLAINED)
So even i was wondering as to why i was not able to pass one test case for this case even though the same code was working in Leetcode. The reason i can think of is that we need to use .equals() rather == because .equals() will check the value of the two objects we are comparing whereas in == operator checks for reference equality.
Below is the Code:
public class Solution {
public static boolean isMirror(BinaryTreeNode<Integer>t1, BinaryTreeNode<Integer> t2){
if(t1==null && t2==null) return true;
if(t1==null || t2==null) return false;
return (t1.data.equals(t2.data)) && isMirror(t1.left,t2.right) && isMirror(t1.right,t2.left);
}
public static boolean isSymmetric(BinaryTreeNode<Integer> root) {
if(root==null) return true;
return isMirror(root,root);
}
}
Interview problems
Java Solution is working in GFG and Leetcode but here one Test case is failing could you please share the failed test case
public class Solution {
public static boolean isSymmetric(BinaryTreeNode<Integer> root) {
// Write your code here.
if(root == null) {
return true;
}
return isIdentical(root.left, root.right);
}
public static boolean isIdentical(BinaryTreeNode<Integer> root1, BinaryTreeNode<Integer> root2) {
if(root1 == null && root2 == null) {
return true;
}
else if(root1 == null && root2 != null) {
return false;
}
else if(root1 != null && root2 == null) {
return false;
}
else {
return ((root1.data == root2.data)
&& isIdentical(root1.left, root2.right)
&& isIdentical(root1.right, root2.left)
);
}
}
Interview problems
Striver's easy recursive solution in C++ | Symmetric tree
my github : https://github.com/yashswag22
bool isSymmetricHelp(BinaryTreeNode<int>* left, BinaryTreeNode<int>* right){
if(left == NULL || right == NULL)
return left == right;
if(left->data != right->data) return false;
return isSymmetricHelp(left->left,right->right) && isSymmetricHelp(left->right,right->left);
}
bool isSymmetric(BinaryTreeNode<int>* root){
return root == NULL || isSymmetricHelp(root->left, root->right);
}
Interview problems
how to solve symmetric tree question , solution in c++
#include<bits/stdc++.h>
// void printMap(const map<int, vector<pair<int, int>>>& nodes) {
// for (const auto& lvl_pair : nodes) {
// int lvl = lvl_pair.first;
// const vector<pair<int, int>>& vec = lvl_pair.second;
// cout << "{" << lvl;
// for (const auto& p : vec) {
// cout << ", {" << p.first << ", " << p.second << "}";
// }
// cout << "}" << endl;
// }
// }
bool isSymmetric(BinaryTreeNode<int>* root)
{
if(root == NULL) return true;
if(root->left == NULL && root->right == NULL) return true;
map <int,vector<pair<int,int>>> nodes;
queue<pair<int,pair<int,BinaryTreeNode<int>*>>> q;
//y,x
//q.push({0,{0,root}});
if(root->left)
q.push({1,{-1,root->left}});
if(root->right)
q.push({1,{1,root->right}});
while(!q.empty()){
pair<int,pair<int,BinaryTreeNode<int>*>> temp = q.front();
q.pop();
int lvl = temp.first;
int x = temp.second.first;
BinaryTreeNode<int>* frontnode = temp.second.second;
if(frontnode != NULL)
nodes[lvl].push_back({x,frontnode->data});
if(frontnode->left)
q.push({lvl+1,{x-1,frontnode->left}});
if(frontnode->right)
q.push({lvl+1,{x+1,frontnode->right}});
}
//printMap(nodes);
for(auto i:nodes){ // give <lvl,vector<pair<int,int>>>
// vector<pair<int,int>> temp = i.second;
int siz = i.second.size();
if(siz%2 != 0) return false;
for(int j=0;j<siz/2;j++){
pair<int,int> temp = i.second[j];
pair<int,int> temp2 = i.second[(siz-1)-j];
if(temp.first != -1*(temp2.first)) return false;
else{
if(temp.second != temp2.second) return false;
}
// cout<<temp.first<<" "<<temp.second<<" ";
// cout<<temp2.first<<" "<<temp2.second;
}
// cout<<endl;
}
return true;
}