0% found this document useful (0 votes)
2 views1 page

Same Tree

The document defines a binary tree node structure and implements a solution to check if two binary trees are identical. The 'check' function recursively compares the nodes of both trees, returning true if they have the same structure and values. The 'isSameTree' function serves as a public interface to invoke the 'check' function.

Uploaded by

Ayush Saklani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Same Tree

The document defines a binary tree node structure and implements a solution to check if two binary trees are identical. The 'check' function recursively compares the nodes of both trees, returning true if they have the same structure and values. The 'isSameTree' function serves as a public interface to invoke the 'check' function.

Uploaded by

Ayush Saklani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

/**

* Definition for a binary tree node.


* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
right(right) {}
* };
*/
class Solution {
public:
bool check(TreeNode* p, TreeNode* q){
bool res,res2;
if(p==NULL && q==NULL) return true;
else if(p==NULL || q==NULL) return false;
else if(p!=NULL && q!=NULL){
if(p->val != q->val){
return false;
}
if(p->val == q->val){
res = check(p->right, q->right);
res2 = check(p->left, q->left);
}
}
if(res== true && res2==true) return true;
return false;
}
bool isSameTree(TreeNode* p, TreeNode* q) {
bool res = check(p,q);
return res;
}
};

You might also like