<分区>
我正在构建一个二叉搜索树,但该函数给出了段错误。我不知道问题出在哪里。
树没有构建 insertintree
中的部分无法正常工作,我已经尝试过一些方法但它没有工作
#include<bits/stdc++.h>
using namespace std;
struct node // structure of node
{
int k;
node* left = NULL;
node* right = NULL;
};
void insertintree(node* root, int key)
{
if (root == NULL)//root addition
{
node* root = new node;
root->k = key;
}
else
{
if (root->k < key) insertintree(root->right, key);
else insertintree(root->left, key);
}
}
void inorder(node* root)
{
if (root != NULL)
{
inorder(root->left);
cout << root->k;
inorder(root->right);
}
}
int main()
{
node* root = NULL;
insertintree(root, 1);
cout << root->k;
}