1
unknown
c_cpp
a year ago
1.9 kB
13
Indexable
Never
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; struct Q{ vector<int> arr; int idx; }; vector<Q> vec; struct node{ vector<int> cor; int idx; node* L; node* R; }; node* root; node* make_node(vector<int> cor, int idx){ node* tmp = new node; tmp->cor = cor; tmp->idx = idx; tmp->L= nullptr; tmp->R = nullptr; return tmp; } bool cmp(Q &a, Q & b){ if(a.arr[1]==b.arr[1]) { return a.arr[0] < b.arr[0]; } return a.arr[1] > b.arr[1]; } void add_node(node* root, Q info){ if(root->cor[0] < info.arr[0]){ if(root->R == nullptr){ root->R = make_node(info.arr, info.idx); }else{ add_node(root->R, info); } }else{ if(root->L == nullptr){ root->L = make_node(info.arr, info.idx); }else{ add_node(root->L, info); } } return; } vector<int> pre_res; void preorder(node* root){ if (root ==nullptr){ return; } // root pre_res.push_back(root->idx +1); preorder(root->L); preorder(root->R); } vector<int> post_res; void postorder(node* root){ if (root ==nullptr){ return; } // root postorder(root->L); postorder(root->R); post_res.push_back(root->idx+1); } vector<vector<int>> solution(vector<vector<int>> nodeinfo) { vector<vector<int>> answer; // init vec.resize(nodeinfo.size()); for(int i = 0;i<nodeinfo.size();i++){ vec[i].arr = nodeinfo[i]; vec[i].idx = i; } sort(vec.begin(), vec.end(), cmp); root = make_node(vec[0].arr,vec[0].idx); for(int i = 1;i<vec.size();i++){ add_node(root, vec[i]); } preorder(root); postorder(root); answer.push_back(pre_res); answer.push_back(post_res); return answer; }