题解 | #二叉搜索树#
二叉搜索树
https://www.nowcoder.com/practice/3d6dd9a58d5246f29f71683346bb8f1b
#include <iostream> using namespace std; struct Node{ char val; Node *l, *r; Node(int _val) : val(_val), l(NULL), r(NULL){} }; void insert(Node* &root, int x){ if(!root) root = new Node(x); else if(x > root->val) insert(root->r, x); else insert(root->l, x); } bool check(Node* a, Node* b){ if(a == NULL && b == NULL) return true; else if(a == NULL || b == NULL) return false; else { if(a->val != b->val) return false; return check(a->l, b->l) && check(a->r, b->r); } } int main() { int n; string s, t; while(cin>>n){ if(n == 0) break; cin>>s; Node *a = NULL; for(int i = 0; i < s.size(); i ++) insert(a, s[i]); while(n--){ cin>>t; Node *b = NULL; for(int i = 0; i < t.size(); i ++) insert(b, t[i]); if(check(a, b)) puts("YES"); else puts("NO"); } } return 0; }