- #include<bits/stdc++.h>
- #include<vector>
- using namespace std;
- typedef struct node* BT;
- struct TreeNode
- {
- int val;
- TreeNode *left;
- TreeNode *right;
- TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- };
- TreeNode *buildBST(TreeNode *root,int x)
- {
- if(root==NULL)
- root=new TreeNode(x);
- else
- {
- if(x<root->val)
- root->left=buildBST(root->left,x);
- else
- root->right=buildBST(root->right,x);
- }
- return root;
- }
- int main()
- {
- TreeNode *root=NULL;
- for(int i=0;i<10;i++)
- {
- int x;
- cin>>x;
- root=buildBST(root,x);
- }
- return 0;}
来源: http://www.bubuko.com/infodetail-3157353.html