欢迎访问LeetCode题解合集
题目描述
给你两棵二叉树的根节点 p
和 q
,编写一个函数来检验这两棵树是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
示例 1:
[HTML_REMOVED]
[HTML_REMOVED]
输入:p = [1,2,3], q = [1,2,3]
输出:true
示例 2:
[HTML_REMOVED]
[HTML_REMOVED]
输入:p = [1,2], q = [1,null,2]
输出:false
示例 3:
[HTML_REMOVED]
[HTML_REMOVED]
输入:p = [1,2,1], q = [1,1,2]
输出:false
提示:
- 两棵树上的节点数目都在范围
[0, 100]
内 - $-10^4 \le Node.val \le 10^4$
题解:
判断两棵树是否相同,对两棵树做同样的操作:深搜(先序,中序,后序)或者 广搜,判断对应的节点是否相同即可。
这里使用 二叉树先序遍历 搜索方式来遍历。
时间复杂度:$O(min(m,n))$
额外空间复杂度:$O(min(m,n))$
/**
* 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 isSameTree(TreeNode* p, TreeNode* q) {
if ( !p && !q ) return true;
if ( !p || !q || p->val != q->val ) return false;
if ( !isSameTree( p->left, q->left ) ) return false;
if ( !isSameTree( p->right, q->right ) ) return false;
return true;
}
};
/*
时间:0ms,击败:100.00%
内存:9.6MB,击败:96.96%
*/
迭代实现 先序遍历 :
写法一:
/**
* 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 isSameTree(TreeNode* p, TreeNode* q) {
stack<TreeNode*> stk1, stk2;
stk1.push( p ), stk2.push( q );
while ( stk1.size() && stk2.size() ) {
p = stk1.top(); stk1.pop();
q = stk2.top(); stk2.pop();
if ( !p && !q ) continue;
if ( !p || !q || p->val != q->val ) return false;
stk1.push( p->right );
stk1.push( p->left );
stk2.push( q->right );
stk2.push( q->left );
}
return true;
}
};
/*
时间:0ms,击败:100.00%
内存:9.9MB,击败:69.88%
*/
写法二:
/**
* 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 isSameTree(TreeNode* p, TreeNode* q) {
stack<TreeNode*> stk;
stk.push( p );
stk.push( q );
TreeNode *t1, *t2;
while ( stk.size() ) {
t1 = stk.top(); stk.pop();
t2 = stk.top(); stk.pop();
if ( !t1 && !t2 ) continue;
if ( !t1 || !t2 || t1->val != t2->val ) return false;
stk.push(t2->right);
stk.push(t1->right);
stk.push(t2->left);
stk.push(t1->left);
}
return true;
}
};
/*
时间:0ms,击败:100.00%
内存:9.8MB,击败:70.76%
*/
写法三:
/**
* 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 isSameTree(TreeNode* p, TreeNode* q) {
stack<TreeNode*> stk1, stk2;
while ( p || stk1.size() || q || stk2.size() ) {
while ( p || q ) {
if ( (p && !q) || (q && !p) ) return false;
if ( p->val != q->val ) return false;
stk1.push( p );
stk2.push( q );
p = p->left;
q = q->left;
}
p = stk1.top(); stk1.pop();
q = stk2.top(); stk2.pop();
p = p->right;
q = q->right;
}
return true;
}
};
/*
时间:0ms,击败:100.00%
内存:9.8MB,击败:80.31%
*/