114. Flatten Binary Tree to Linked List
Medium
Given a binary tree, flatten it to a linked list in-place.
For example, given the following tree:
1 / \ 2 5 / \ \ 3 4 6
The flattened tree should look like:
1
 \
  2
   \
    3
     \
      4
       \
        5
         \
          6
本题就是树转链表,C++好久没写,连类都忘了,不好,打比赛还是得靠C++大杀器,反思
对树的遍历就有递归和非递归两种方法(从今往后要恢复英语写法)From now on
recursion way is simple and easy to understand:
1.when read the code or new knowledge Do it as quick as possible is important than to make the
definition clear.
2.I forget to make root->left=NULL when I use the value of it.Cause the address problem,although 
I didn't need to direct operate left tree.But don't delete it will cause the recursion cannot stop
so finally cause problem
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void flatten(TreeNode* root) { if(!root) return; if(root->left) flatten(root->left); if(root->right) flatten(root->right); TreeNode *tmp=root->right; root->right=root->left; root->left=NULL; while(root->right) root=root->right; root->right=tmp; } };
In order to speed up my code,wrapper function is important.So I write a Tree Class to deal with read write and sort of the tree.once and for all.In order to get familiar with python and C++ so use both of them.
115. Distinct Subsequences
Hard
Given a string S and a string T, count the number of distinct subsequences of S which equals T.
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).
Example 1:
Input: S ="rabbbit", T ="rabbit" Output: 3Explanation: As shown below, there are 3 ways you can generate "rabbit" from S. (The caret symbol ^ means the chosen letters)rabbbit^^^^ ^^rabbbit^^ ^^^^rabbbit^^^ ^^^
Example 2:
Input: S ="babgbag", T ="bag" Output: 5Explanation: As shown below, there are 5 ways you can generate "bag" from S. (The caret symbol ^ means the chosen letters)babgbag^^ ^babgbag^^ ^babgbag^ ^^babgbag^ ^^babgbag^^^
This is hard level,compare sequence,I used to thought solve it as Trie Tree.But actually this
solved by Special DP In some ways.complex algorithm and complex data structure maybe can
replace each other.
This code use many novel approach,
only problem is this code can run in my local but runtime error in LC.it will show overflow.So
I change the array to the long type can solve this problem.But this code's apperence in space and
time complexity is ordinary.
class Solution{ public: int numDistinct(string s,string t) { int ls=s.length(); int lt=t.length(); //must add 1 match the situation not letter ¡ª¡ª¡·chart[0][0] vector<vector<int>> dp(lt+1,vector<int>(ls+1)); fill(begin(dp[0]),end(dp[0]),1); for(int i=1;i<=lt;++i) for(int j=1;j<=ls;++j) // dp and s use different index system,minus 1 dp[i][j]=dp[i][j-1]+(s[j-1]==t[i-1]?dp[i-1][j-1]:0); return dp[lt][ls]; // vector<vector<int>>() } }; int main() { // int lt,ls; // cin>>lt>>ls; Solution s1; string S,T; S = "xslledayhxhadmctrliaxqpokyezcfhzaskeykchkmhpyjipxtsuljkwkovmvelvwxzwieeuqnjozrfwmzsylcwvsthnxujvrkszqwtglewkycikdaiocglwzukwovsghkhyidevhbgffoqkpabthmqihcfxxzdejletqjoxmwftlxfcxgxgvpperwbqvhxgsbbkmphyomtbjzdjhcrcsggleiczpbfjcgtpycpmrjnckslrwduqlccqmgrdhxolfjafmsrfdghnatexyanldrdpxvvgujsztuffoymrfteholgonuaqndinadtumnuhkboyzaqguwqijwxxszngextfcozpetyownmyneehdwqmtpjloztswmzzdzqhuoxrblppqvyvsqhnhryvqsqogpnlqfulurexdtovqpqkfxxnqykgscxaskmksivoazlducanrqxynxlgvwonalpsyddqmaemcrrwvrjmjjnygyebwtqxehrclwsxzylbqexnxjcgspeynlbmetlkacnnbhmaizbadynajpibepbuacggxrqavfnwpcwxbzxfymhjcslghmajrirqzjqxpgtgisfjreqrqabssobbadmtmdknmakdigjqyqcruujlwmfoagrckdwyiglviyyrekjealvvigiesnvuumxgsveadrxlpwetioxibtdjblowblqvzpbrmhupyrdophjxvhgzclidzybajuxllacyhyphssvhcffxonysahvzhzbttyeeyiefhunbokiqrpqfcoxdxvefugapeevdoakxwzykmhbdytjbhigffkmbqmqxsoaiomgmmgwapzdosorcxxhejvgajyzdmzlcntqbapbpofdjtulstuzdrffafedufqwsknumcxbschdybosxkrabyfdejgyozwillcxpcaiehlelczioskqtptzaczobvyojdlyflilvwqgyrqmjaeepydrcchfyftjighntqzoo"; T="rwmimatmhydhbujebqehjprrwfkoebcxxqfktayaaeheys"; cout<<s1.numDistinct(S,T)<<endl; // vector<vector<int>> dp(lt + 1, vector<int>(ls + 1)); return 0; }
 
    愿为天下目,萃聚六路华
                    
                
                
            
        
浙公网安备 33010602011771号