PAT-A -1043 Is It a Binary Search Tree (25)【二叉查找树BST】

1043 Is It a Binary Search Tree (25)

模板题:
树的创建(插入节点,新建节点)
树的遍历(前中后遍历都是DFS)

#include
using namespace std;

struct node
{
    int data;
    node *left,*right;
};

void insert(node* &root,int data)
{
    if(root ==NULL)
    {
        root = new node;
        root->data =data;
        root->left=root->right =NULL;
        return ;
    }
    if(datadata)
        insert(root->left,data);
    else
        insert(root->right,data);
}

void preOrder(node* root, vector&vi)
{
    if(root == NULL)
        return;
    vi.push_back(root->data);
    preOrder(root->left,vi);
    preOrder(root->right,vi);
}

void preOrderMirror(node* root,vector&vi)
{
    if(root == NULL)
        return;
    vi.push_back(root->data);
   preOrderMirror(root->right,vi);
   preOrderMirror(root->left,vi);
}

void postOrder(node* root,vector&vi)
{
    if(root == NULL)
        return;
postOrder(root->left,vi);
postOrder(root->right,vi);
    vi.push_back(root->data);

}

void postOrderMirror(node* root,vector&vi)
{
    if(root == NULL)
        return;
    postOrderMirror(root->right,vi);
    postOrderMirror(root->left,vi);
    vi.push_back(root->data);
}

vectororigin,pre,preM,post,postM;

int main()
{
    int n,data;
    node* root=NULL;
    scanf("%d",&n);
    for(int i=0; i

你可能感兴趣的:(PAT-A -1043 Is It a Binary Search Tree (25)【二叉查找树BST】)