PTA Delete Nodes in a Binary Search Tree

209 阅读1分钟

Given a binary search tree with no same keys, you should output the level-order traversal sequence of this tree after some nodes are deleted.

When trying to remove a node from the tree, we follow the rules listed below:

  • If the node to be deleted is a leaf node, remove it directly;
  • If the node has left subtree, set its key to be the maximum key in its left subtree and remove the node in its left subtree with maximum key;
  • If the node doesn’t have left subtree but has right subtree, set its key to be the minimum key in its right subtree and remove the node in its right subtree with minimum key.

Input Specification:

Each input file contains one test case. The first line of each test contains a number N (1<N<100, the count of nodes in the binary search tree). The second line gives the pre-order traversal sequences of the binary search tree, consisting of N different numbers separated by a space. The third line gives a number K (0<K<N, the count of nodes to be deleted). And the next line gives K numbers, representing keys of nodes to be deleted. It is guaranteed that no delete operation will fail.

Output Specification:

Output the level-order traversal sequence of the tree. Numbers in the sequences must be separated by a space, and no extra space is allowed at the end of line.

Sample Input:

7
4 2 1 3 6 5 7
2
3 6
结尾无空行

Sample Output:

4 2 5 1 7
结尾无空行

代码:

#include <bits/stdc++.h>
using namespace std;

int n, m;
int a[110];
struct node
{
    node *L, *R;
    node *pre;
    node *parent;
    int val;
};

unordered_map<int, node *> h;

node *build(int l, int r, node *p)
{
    if (r < l)
        return NULL;
    int k;
    for (k = l + 1; k <= r; k++)
        if (a[k] > a[l])
            break;
    node *root = new node;
    root->val = a[l];
    root->parent = p;
    node *t = root;
    h[a[l]] = root;
    root->L = build(l + 1, k - 1, t);
    t = root;
    root->R = build(k, r, t);
    return root;
}

node *findmax(node *root)
{
    if (!root->R)
        return root;
    else
        return findmax(root->R);
}

node *findmin(node *root)
{
    if (!root->L)
        return root;
    else
        return findmax(root->L);
}

void del(node *d)
{
    if (!d->L && !d->R)
    {
        node *p = d->parent;
        if (p->val > d->val)
            p->L = NULL;
        else
            p->R = NULL;
    }
    else if (!d->L)
    {
        node *find = findmin(d->R);
        int t = find->val;
        del(find);
        d->val = t;
        h[t] = d;
    }
    else
    {
        node *find = findmax(d->L);
        int t = find->val;
        del(find);
        d->val = t;
        h[t] = d;
    }
}

void printans(node *newnode)
{
    int flag = 0;
    node *root = newnode->R;
    if (!root)
        return;
    queue<node *> q;
    q.push(root);
    while (q.size())
    {
        auto t = q.front();
        q.pop();
        if (!flag)
            cout << t->val, flag++;
        else
            cout << ' ' << t->val;
        if (t->L)
            q.push(t->L);
        if (t->R)
            q.push(t->R);
    }
    return;
}

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    node *newnode = new node;
    newnode->val = -1;
    node *root = build(1, n, newnode);
    newnode->R = root;
    cin >> m;
    while (m--)
    {
        int x;
        cin >> x;
        del(h[x]);
    }
    printans(newnode);
    return 0;
}

提交结果:

2.png