import java.util.*;
import java.lang.*;
import java.io.*;


class Main {
    static class Node {
        Node left, right; 
        int value; 
        Node(int value) {
            this.value = value;
            this.left = null;
            this.right = null;
        }
    }

    public static Node insert(Node node, int value) {
        if (node == null) {
            return new Node(value);
        }

        if(node.value > value) {
           node.left = insert(node.left, value);
        }
        
        if(node.value < value) {
           node.right = insert(node.right, value);
        }
        return node;
    }

    public static Node erase(Node node, int value) {
        Node parent = node;
        Node scout = node.right;
        // 이렇게 객체 할당하면 이거 자체가 node를 바꾸지는 않지 않나?하는 의심이 드는데...배열처럼 메모리주소를 바라보나?... 으음...
        
        
       
        while (scout.left != null) {
            parent = scout;
            scout = scout.left;
            
        }
        //scout노드의 오른쪽 노드가 있을 경우가 좀 애매하긴 한데
        // 이 문제 자체에서는 필요없으려나 했지만 필요있다. 오른쪽에서 몇번이고 추출할 수 있으니 
        // 부모 노드의 왼쪽이 나니까 나 대신에 내 오른쪽자신 연결하자.
        if (scout.right != null) {
            parent.left = scout.right;
        }
        
        node.value = scout.value;
        
        scout= null;
        return node;
    }
    
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        Node root = null;
        for (int i=0; i<N; i++) {
            int a = Integer.parseInt(st.nextToken());
            root = insert(root, a);
        }

        int M = Integer.parseInt(br.readLine());
        st = new StringTokenizer(br.readLine());
        
        for (int j=0; j<M; j++) {
            int t = Integer.parseInt(st.nextToken());
            erase(root, t);
        }
         
        
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: