/**
* 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) {
if (p == NULL && q == NULL)
return true;
if ((p != NULL && q == NULL) || (p == NULL && q != NULL) || (p->val != q->val))
return false;
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};
static int x=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
LetCode 100. 相同的树
最新推荐文章于 2025-08-19 21:57:22 发布
本文介绍了一种算法,用于判断两个二叉树结构是否完全相同,包括它们的节点值和连接方式。通过递归方法比较两棵树的每个节点来实现这一目标。
336

被折叠的 条评论
为什么被折叠?



