题解 | #二叉搜索树#
二叉搜索树
https://www.nowcoder.com/practice/3d6dd9a58d5246f29f71683346bb8f1b
#include <iostream>
using namespace std;
struct Node {
    char data;
    Node* left;
    Node* right;
    Node(char c):data(c), left(NULL), right(NULL){}
};
Node* insert(Node* root, char c){
    if(root == NULL) root = new Node(c);
    else if(c < root->data) root->left = insert(root->left, c);
    else root->right = insert(root->right, c);
    return root;
}
bool cmp(Node* root1, Node* root2){
    if(root1 == NULL && root2 == NULL)
        return true;
    else if(root1 == NULL&&root2!=NULL || root1!=NULL&&root2==NULL)
        return false;
    else
        return cmp(root1->left, root2->left)&&cmp(root1->right, root2->right);
}
int main() {
    int n;
    string str;
    while(cin >> n){
        cin >> str;
        Node* root1 = NULL;
        for(int i = 0; i < str.length(); i ++) 
            root1 = insert(root1, str[i]);
        while(n --){
            cin >> str;
            Node* root2 = NULL;
            for(int i = 0; i < str.length(); i ++) 
                root2 = insert(root2, str[i]);
            
            bool result = cmp(root1, root2);
            
            if(result) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
    return 0;
}
// 64 位输出请用 printf("%lld")
 查看13道真题和解析
查看13道真题和解析
 投递多益网络等公司10个岗位
投递多益网络等公司10个岗位