Recover Binary Search Tree
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
void dfs(TreeNode* A, int& small, vector<int>& res) { if(A == NULL) return; dfs(A->left, small, res); if(small > A->val) { if(res.empty()) { res.push_back(small); res.push_back(A->val); } else res.back() = A->val; } small = A->val; dfs(A->right, small, res); } vector<int> Solution::recoverTree(TreeNode* A) { vector<int> res; int small = INT_MIN; dfs(A,small,res); sort(begin(res),end(res)); return res; }
|