【手写代码】建立二叉搜索树

#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;}
posted @ 2019-08-16 09:53  西*风  阅读(490)  评论(0编辑  收藏  举报