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.

解决思路:

DFS遍历两颗二叉树的结点,只要有一个不同就返回false

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* 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 == NULL)
{
if (q == NULL)
return true;
return false;
}
if(q == NULL)
{
if (p == NULL)
return true;
return false;
}
if (p->val != q->val)
return false;
bool lsame = isSameTree(p->left,q->left);
bool rsame = isSameTree(p->right,q->right);
if(lsame==false||rsame==false)
return false;
return true;

}
};