void InOrderTravel(TreeNode* node, vector<TreeNode*>& list, vector<int>& vals) { if(node == NULL) return; InOrderTravel(node->left, list, vals); list.push_back(node); vals.push_back(node->val); InOrderTravel(node->right, list, vals); }
void InOrderTravel(TreeNode* root, vector<TreeNode*>& q, vector<int>& val) { if (NULL == root) { return; } InOrderTravel(root->left, q, val); q.push_back(root); val.push_back(root->val); InOrderTravel(root->right, q, val); return; }
void recoverTree(TreeNode* root) { InOrderTravel(root, list, vals); sort(vals.begin(), vals.end()); for(int i =0; i< list.size(); i++) { list[i]->val = vals[i]; } }
void recoverTree(TreeNode* root) { vector<TreeNode*> q; vector<int> val; InOrderTravel(root, q, val); sort(val.begin(), val.end()); int size = q.size(); for (int i = 0; i < size; i++) { q[i]->val = val[i]; } return; }