题解 | #二叉排序树#
二叉排序树
https://www.nowcoder.com/practice/30a0153649304645935c949df7599602
#include <iostream> #include<vector> using namespace std; typedef struct Binode { int data; struct Binode* lchild; struct Binode* rchild; Binode(int x): data(x), lchild(NULL), rchild(NULL) {} }; void insertTree(int x, Binode* b) { if (x > b->data) { if (b->rchild == NULL) { Binode* node = new Binode(x); b->rchild = node; cout << b->data; } else { Binode* temporary = b; insertTree(x, temporary->rchild); } } else { if (b->lchild == NULL) { Binode* node = new Binode(x); b->lchild = node; cout << b->data; } else { insertTree(x, b->lchild); } } } int main() { int num0; vector<int> value; while (cin >> num0) { int num = num0; int x; cin >> x; Binode* node = new Binode(x); cout << -1 << "\n"; Binode* root = node; //保存根节点 while (num - 1 > 0) { cin >> x; insertTree(x, node); node = root; // cout<<node->data; num--; cout << "\n"; } } return 0; }