# edited page ```cpp= #include <vector> #include <iostream> #include <climits> #include <cmath> #include <string> #include <algorithm> using namespace std; int depth = INT_MIN; int left_p = INT_MIN; struct node{ node* left; node* right; int val; node(int vval){ left = NULL; right = NULL; val = vval; } }; void insert(node* root,int tmp){ if(root == NULL){ root = new node(tmp); } else{ if(tmp > root->val){ if(root->right == NULL) root->right = new node(tmp); else{ insert(root->right,tmp); return; } } else if(tmp < root->val){ if(root->left == NULL) root->left = new node(tmp); else{ insert(root->left,tmp); return; } } else{ return; } } } void dfs(node* root, int deep){ depth = max(depth, deep); //cout << root->val << " "; if(root->left != NULL) dfs(root->left, deep + 1); if(root->right != NULL) dfs(root->right, deep + 1); //else // here may be a little tricky, I don't know why it may be runtime error without else //delete root; return; } void print_tree(node* root,int row, int num,int col, const vector<int>& count, vector<string>& ans){ // edited ans[row][col] = 'X'; if(root->left != NULL){ int row_tmp = row + 1; int col_tmp = col - 1; for(int i = 1;i < count[num];++i) ans[row_tmp++][col_tmp--] = '/'; print_tree(root->left, row_tmp, num - 1, col_tmp, count, ans); } if(root->right != NULL){ int row_tmp = row + 1; int col_tmp = col + 1; for(int i = 1;i < count[num];++i) ans[row_tmp++][col_tmp++] = '\\'; print_tree(root->right, row_tmp, num - 1, col_tmp, count, ans); } return; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); vector<int> count(21, 0); for(int i = 0;i < 21;++i){ count[i] = pow(2, i); } int n,tmp; node* root = NULL; while(cin >> n){ for(int i = 0;i < n;++i){ cin >> tmp; if(i == 0) root = new node(tmp); else insert(root,tmp); } dfs(root, 1); node* tmp2 = root; left_p = 1; while(tmp2->left != NULL){ ++left_p; tmp2 = tmp2->left; } int f_space = 0; int tem = depth; --tem; for(int i = left_p - 1;i > 0;--i) f_space += count[--tem]; vector<string> ans(pow(2, depth - 1)); // edited for (auto& S : ans){ // edited S.resize(pow(2, depth - 1) * 2 + 1); // edited fill(S.begin(), S.end(), ' '); // edited } // edited print_tree(root,0 , depth - 2, f_space, count, ans); //cout << depth << " " << left_p << " " << f_space << endl; for (auto& S : ans) // edited for (auto rI = S.rbegin(); *rI == ' '; rI++) // edited *rI = '\0'; // edited for (const auto& S : ans) // edited cout << S.c_str() << '\n'; // edited depth = INT_MIN; } return 0; } ```