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.
/**
* Definition for binary tree
* 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) {
vector<int > v1,v2;
dfs1(p,v1);
dfs1(q,v2);
if(v1==v2)
return true;
else return false;
}
private:
void dfs1(TreeNode *root,vector<int> & v)
{
if(root==NULL)
{
v.push_back(0);
return ;
}
v.push_back(root->val);
dfs1(root->left,v);
dfs1(root->right,v);
}
};