我正在try 实现一个函数,该函数判断两个二进制搜索树是否相等, node 的顺序无关紧要.但我的实现不起作用.
I am not allowed to flatten the trees into arrays.
这是我到目前为止得到的:
int isIdentical(struct Node* root1, struct Node* root2)
{
if (root1 == NULL && root2 == NULL)
return 1;
else if (root1 == NULL || root2 == NULL)
return 0;
else {
if (root1->data == root2->data && isIdentical(root1->left, root2->left)
&& isIdentical(root1->right, root2->right))
return 1;
else
return 0;
}
}
当提供包含 node tree A = 2 4 5 6
和Tree B = 2 5 4 6
的树时,输出应为:
1
,意味着它们相等,但我得到的是0
.我不确定我错在哪里.
node 是如何实现的:
struct Node {
int data;
struct Node* left;
struct Node* right;
};