如何在整个二叉树中搜索x下的整数并将它们写入C中的txt文件

How to search the entire binary tree for ints under x and write them to txt file in C

如何在整个二叉树中搜索x下的整数并将其写入C中的txt文件。 我尝试了这段代码,但只搜索了树的一部分。

void save_tree_to(FILE *out, bst *root, unsigned x)
{
    if (root == NULL)
    {
        return;
    }
    save_tree_to(out, root->left, x);
    if (root->price <= x)
    {
        save_item_to(out, root);
    }
    save_tree_to(out, root->right, x);
}

void save_item_to(FILE *out, bst *m)
{
    fprintf(out, "%d\n", m->price);
}

具有根 >x 的子树可以很好地包含元素 ≤x,您过早终止搜索。

仅当您到达 >x.

时才停止搜索