Symmetric Tree @LeetCode
PROBLEM :
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2 2
\ \
3 3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Recursive Solution)
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL)
return true ;
if(Symmetric(root->left,root->right))
return true ;
return false ;
}
bool Symmetric(TreeNode* a,TreeNode* b){
if(a==NULL&&b==NULL)
return true ;
if(a==NULL||b==NULL)
return false ;
if((a->val==b->val)&&(Symmetric(a->left,b->right)&&Symmetric(a->right,b->left)))
return true ;
return false ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Iterative Solution - using Stack STL)
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL)
return true ;
stack<TreeNode*> a ;
stack<TreeNode*> b ;
a.push(root) ;
b.push(root) ;
while(!a.empty()&&!b.empty()){
TreeNode* x ;
TreeNode* y ;
x=a.top() ;
y=b.top() ;
a.pop() ;
b.pop() ;
if(x==NULL&&y==NULL)
continue ;
if(x==NULL||y==NULL)
return false ;
if(x->val!=y->val)
return false ;
a.push(x->left) ;
a.push(x->right) ;
b.push(y->right) ;
b.push(y->left) ;
}
return true ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Iterative Solution - using Queue STL)
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL)
return true ;
queue<TreeNode*> a ;
queue<TreeNode*> b ;
a.push(root) ;
b.push(root) ;
while(!a.empty()&&!b.empty()){
TreeNode* x ;
TreeNode* y ;
x=a.front() ;
y=b.front() ;
a.pop() ;
b.pop() ;
if(x==NULL&&y==NULL)
continue ;
if(x==NULL||y==NULL)
return false ;
if(x->val!=y->val)
return false ;
a.push(x->left) ;
a.push(x->right) ;
b.push(y->right) ;
b.push(y->left) ;
}
return true ;
}
};
---------------------------------------------------------------------------------
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2 2
\ \
3 3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Recursive Solution)
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL)
return true ;
if(Symmetric(root->left,root->right))
return true ;
return false ;
}
bool Symmetric(TreeNode* a,TreeNode* b){
if(a==NULL&&b==NULL)
return true ;
if(a==NULL||b==NULL)
return false ;
if((a->val==b->val)&&(Symmetric(a->left,b->right)&&Symmetric(a->right,b->left)))
return true ;
return false ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Iterative Solution - using Stack STL)
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL)
return true ;
stack<TreeNode*> a ;
stack<TreeNode*> b ;
a.push(root) ;
b.push(root) ;
while(!a.empty()&&!b.empty()){
TreeNode* x ;
TreeNode* y ;
x=a.top() ;
y=b.top() ;
a.pop() ;
b.pop() ;
if(x==NULL&&y==NULL)
continue ;
if(x==NULL||y==NULL)
return false ;
if(x->val!=y->val)
return false ;
a.push(x->left) ;
a.push(x->right) ;
b.push(y->right) ;
b.push(y->left) ;
}
return true ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Iterative Solution - using Queue STL)
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL)
return true ;
queue<TreeNode*> a ;
queue<TreeNode*> b ;
a.push(root) ;
b.push(root) ;
while(!a.empty()&&!b.empty()){
TreeNode* x ;
TreeNode* y ;
x=a.front() ;
y=b.front() ;
a.pop() ;
b.pop() ;
if(x==NULL&&y==NULL)
continue ;
if(x==NULL||y==NULL)
return false ;
if(x->val!=y->val)
return false ;
a.push(x->left) ;
a.push(x->right) ;
b.push(y->right) ;
b.push(y->left) ;
}
return true ;
}
};
---------------------------------------------------------------------------------
Comments
Post a Comment