题解 | #二叉排序树#函数要加&引用,不然很容易错
二叉排序树
https://www.nowcoder.com/practice/30a0153649304645935c949df7599602
#include <iostream> using namespace std; struct BTree { int val; struct BTree* lchild,*rchild; BTree(int x):val(x),lchild(nullptr),rchild(nullptr){} }; void Insert(BTree*&root,int n,int pre) { if(root==nullptr) { root=new BTree(n); cout<<pre<<endl; } else if(root->val<n)Insert(root->rchild, n, root->val); else if(root->val>n)Insert(root->lchild, n, root->val); } int main() { BTree*T=nullptr; int n; cin>>n; while(cin>>n) { Insert(T,n,-1); } return 0; } // 64 位输出请用 printf("%lld")