Я создал связанный список в C. Я динамически выделяю память как
'' '
struct node* createNode(value) {
struct node* newNode =
malloc(sizeof(struct node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* insertLeft(struct node* root, int value) {
root->left = createNode(value);
return root->left;
}
int main() {
struct node* root = createNode(1);
insertLeft(root,12);
//Create more nodes
postorder(root);
}
void postorder(struct node* root) {
if (root == NULL) return;
postorder(root->left);
postorder(root->right);
printf("%d ->", root->data);
}
' ''
Теперь узел root, который я получаю после вставки, принадлежит только что вставленному элементу. Потому что я возвращаю этот новый узел. Почему при обходе самый верхний root передается в postorder.
Также, если я не верну этот новый узел, он все еще должен существовать в памяти, так зачем возвращать?