题目
给定两个二叉树,编写一个函数来检验它们是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
示例
- 示例 1:
输入: 1 1
/ \ / \
2 3 2 3
[1,2,3], [1,2,3]
输出: true
- 示例 2:
输入: 1 1
/ \
2 2
[1,2], [1,null,2]
输出: false
- 示例 3
输入: 1 1
/ \ / \
2 1 1 2
[1,2,1], [1,1,2]
输出: false
C++代码
看的别人的程序。。。
递归深度搜索,检查对应的结点是否一样,不一样则返回false。
/**
* 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)
{
return isSamePoint(p,q);
}
bool isSamePoint(TreeNode* p, TreeNode* q)
{
if(p==NULL&&q==NULL) return true;
if(p==NULL||q==NULL) return false;
if(p->val!=q->val) return false;
else return isSamePoint(p->left,q->left)&&isSamePoint(p->right,q->right);
}
};