#10418. szjy_6_树_038
szjy_6_树_038
szjy_6_树_038
函数 same 判断两棵二叉树的结构和对应结点值是否都相同。程序输出是( )。
#include <iostream>
using namespace std;
struct Node {
int value;
Node* left = nullptr;
Node* right = nullptr;
};
bool same(Node* a, Node* b) {
if (a == nullptr || b == nullptr) return a == b;
return a->value == b->value
&& same(a->left, b->left)
&& same(a->right, b->right);
}
int main() {
Node a1{1}, a2{2}, a3{3};
Node b1{1}, b2{2}, b3{4};
a1.left = &a2; a1.right = &a3;
b1.left = &b2; b1.right = &b3;
cout << same(&a1, &b1);
}
{{ select(1) }}
- 2
- 0
- 1
- 3