#10394. szjy_6_树_014

szjy_6_树_014

szjy_6_树_014

阅读代码,程序输出是( )。

#include <iostream>
using namespace std;

struct Node {
int value;
Node* left;
Node* right;
Node(int x) : value(x), left(nullptr), right(nullptr) {}
};

Node* insert(Node* root, int x) {
if (root == nullptr) return new Node(x);
if (x < root->value) root->left = insert(root->left, x);
else root->right = insert(root->right, x);
return root;
}

void print(Node* root) {
if (root == nullptr) return;
print(root->left);
cout << root->value << ' ';
print(root->right);
}

int main() {
int a[] = {8, 3, 10, 1, 6, 14, 4};
Node* root = nullptr;
for (int x : a) root = insert(root, x);
print(root);
}

{{ select(1) }}

  • 8 3 1 6 4 10 14
  • 1 3 4 6 8 10 14
  • 1 4 6 3 14 10 8
  • 14 10 8 6 4 3 1