# 100. Same Tree

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 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*> pque,qque;
if (p&&q)
{
pque.push(p);
qque.push(q);
}
else if (!p&&!q)  return true;
else return false;
while (!pque.empty()&&!qque.empty())
{
if (pque.front()->val!=qque.front()->val)
return false;
else
{
TreeNode* pp=pque.front(),*qq=qque.front();
pque.pop();
qque.pop();
if (pp->left&&qq->left)
{
pque.push(pp->left);
qque.push(qq->left);
}
else if(!pp->left&&!qq->left) {}
else return false;
if (pp->right&&qq->right)
{
pque.push(pp->right);
qque.push(qq->right);
}
else if(!pp->right&&!qq->right) {}
else return false;
}
}
if (!pque.empty()||!qque.empty())
return false;
return true;
}
};

/**
* 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) {
if(!p && !q)
return true;
else if(!p && q)
return false;
else if(p && !q)
return false;
else
{
if(p->val != q->val)
return false;
else
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
}
};

#### 100. Same Tree [easy] (Python)

2016-05-13 22:13:29

#### LeetCode(100)Same Tree

2014-01-09 06:41:21

#### LeetCode-Same Tree(java)

2014-05-14 09:56:06

#### [LeetCode]100. Same Tree(相同树)

2017-04-20 00:34:36

#### 100. Same Tree Leetcode Python

2015-01-15 23:33:34

#### [leetcode-100]Same Tree(C)

2015-08-11 10:32:20

#### LeetCode 100 — Same Tree（C++ Java Python）

2014-04-01 20:22:21

#### Leetcode#100. Same Tree(判断两个二叉树相同)

2017-09-27 12:29:08

#### [LeetCode]--100. Same Tree

2016-10-04 08:32:50

#### 【Leetcode】100. Same Tree

2016-03-25 13:20:50