Same Tree @LeetCode
PROBLEM :
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
--------------------------------------------------------------------------------
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 isSameTree(TreeNode* p, TreeNode* q) {
if(p==NULL&&q==NULL)
return true ;
if(p==NULL||q==NULL)
return false ;
if((p->val==q->val)&&(isSameTree(p->left,q->left)&&isSameTree(p->right,q->right)))
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 isSameTree(TreeNode* p, TreeNode* q) {
stack<TreeNode*> a ;
stack<TreeNode*> b ;
if(p!=NULL)
a.push(p) ;
if(q!=NULL)
b.push(q) ;
while(!a.empty()&&!b.empty()){
TreeNode *x=a.top() ;
TreeNode *y=b.top() ;
a.pop() ;
b.pop() ;
if(x->val!=y->val)
return false ;
if(x->left!=NULL)
a.push(x->left) ;
if(y->left!=NULL)
b.push(y->left) ;
if(a.size()!=b.size())
return false ;
if(x->right!=NULL)
a.push(x->right) ;
if(y->right!=NULL)
b.push(y->right) ;
if(a.size()!=b.size())
return false ;
}
if(a.size()!=b.size())
return false ;
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 isSameTree(TreeNode* p, TreeNode* q) {
queue<TreeNode*> a ;
queue<TreeNode*> b ;
if(p!=NULL)
a.push(p) ;
if(q!=NULL)
b.push(q) ;
while(!a.empty()&&!b.empty()){
TreeNode *x=a.front() ;
TreeNode *y=b.front() ;
a.pop() ;
b.pop() ;
if(x->val!=y->val)
return false ;
if(x->left!=NULL)
a.push(x->left) ;
if(y->left!=NULL)
b.push(y->left) ;
if(a.size()!=b.size())
return false ;
if(x->right!=NULL)
a.push(x->right) ;
if(y->right!=NULL)
b.push(y->right) ;
if(a.size()!=b.size())
return false ;
}
if(a.size()!=b.size())
return false ;
return true ;
}
};
---------------------------------------------------------------------------------
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
--------------------------------------------------------------------------------
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 isSameTree(TreeNode* p, TreeNode* q) {
if(p==NULL&&q==NULL)
return true ;
if(p==NULL||q==NULL)
return false ;
if((p->val==q->val)&&(isSameTree(p->left,q->left)&&isSameTree(p->right,q->right)))
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 isSameTree(TreeNode* p, TreeNode* q) {
stack<TreeNode*> a ;
stack<TreeNode*> b ;
if(p!=NULL)
a.push(p) ;
if(q!=NULL)
b.push(q) ;
while(!a.empty()&&!b.empty()){
TreeNode *x=a.top() ;
TreeNode *y=b.top() ;
a.pop() ;
b.pop() ;
if(x->val!=y->val)
return false ;
if(x->left!=NULL)
a.push(x->left) ;
if(y->left!=NULL)
b.push(y->left) ;
if(a.size()!=b.size())
return false ;
if(x->right!=NULL)
a.push(x->right) ;
if(y->right!=NULL)
b.push(y->right) ;
if(a.size()!=b.size())
return false ;
}
if(a.size()!=b.size())
return false ;
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 isSameTree(TreeNode* p, TreeNode* q) {
queue<TreeNode*> a ;
queue<TreeNode*> b ;
if(p!=NULL)
a.push(p) ;
if(q!=NULL)
b.push(q) ;
while(!a.empty()&&!b.empty()){
TreeNode *x=a.front() ;
TreeNode *y=b.front() ;
a.pop() ;
b.pop() ;
if(x->val!=y->val)
return false ;
if(x->left!=NULL)
a.push(x->left) ;
if(y->left!=NULL)
b.push(y->left) ;
if(a.size()!=b.size())
return false ;
if(x->right!=NULL)
a.push(x->right) ;
if(y->right!=NULL)
b.push(y->right) ;
if(a.size()!=b.size())
return false ;
}
if(a.size()!=b.size())
return false ;
return true ;
}
};
---------------------------------------------------------------------------------
Comments
Post a Comment