Google interview question

Tree + Hashmap , DP on subsequences (Optimised Version)