The problem is not hard if you know to find the LIS in O(nlogn) time. Combining LIS and tree problem is quite interesting.
The key part of this problem is how to backtrack. I used vector so the backtrack part is a little bit more cumbersome than regular array’s since you have to record whether you add a new element or replace a element. My approach is that if we add a new element, set flag to -1 otherwise set flag to the old number.