# [sword finger Offer] search and backtracking algorithm

#### Sword finger Offer 37. Serialized binary tree

Please implement two functions to serialize and deserialize the binary tree respectively.

You need to design an algorithm to realize the serialization and deserialization of binary tree. There is no restriction on the execution logic of your sequence / deserialization algorithm. You only need to ensure that a binary tree can be serialized into a string and deserialize the string into the original tree structure.

**Prompt: * * the input / output format is the same as that currently used by LeetCode. For details, see Format of LeetCode serialized binary tree . You don't have to take this approach, you can also take other methods to solve the problem.

Example: ```Input: root = [1,2,3,null,null,4,5]
Output:[1,2,3,null,null,4,5]
```
```/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
string ans = "[";
queue<TreeNode*> q;
q.push(root);
bool tag = true;
while(q.empty() == false && tag){
tag = false;
for(int i = q.size(); i > 0; -- i){
TreeNode *cur = q.front();
q.pop();
if(cur != nullptr){
ans += (to_string(cur->val));
ans += ',';
q.push(cur->left);
if(cur->left != nullptr || cur->right != nullptr)tag = true;
q.push(cur->right);
}else ans += "null,";
}
}
ans[ans.size() - 1] = ']';
return ans;
}

// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
// cout << 2 << endl;
if(data == "[]")return nullptr;
int n = data.size();
string str = "";
vector<string> ns;
for(int i = 1; i < n - 1; ++ i){
if(data[i] == ','){
ns.push_back(str);
str = "";
}else str += data[i];
}
ns.push_back(str);
queue<TreeNode*> q;
TreeNode *ans = nullptr;
for(int i = 0; i < ns.size(); ++ i){
if(q.empty()){
if(ns[i] != "null"){
ans = new TreeNode(atoi(ns[i].c_str()));
q.push(ans);
}
}else{
TreeNode *cur = q.front();
q.pop();
if(ns[i] != "null"){
TreeNode *left = new TreeNode(atoi(ns[i].c_str()));
cur->left = left;
q.push(left);
}else cur->left = nullptr;
i ++;
if(ns[i] != "null"){
TreeNode *right = new TreeNode(atoi(ns[i].c_str()));
cur->right = right;
q.push(right);
}else cur->right = nullptr;
}
}
return ans;
}
};

// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
```

Algorithm idea

For the conversion of binary tree, we should pay attention to the format of Leetcode serialization, which is difficult to control, such as the display of empty nodes. The data structure used in the conversion process is queue, and then it is the fixed framework of bfs, traversing layer by layer. #### Sword finger Offer 38. Arrangement of strings

Enter a string and print out all the arrangements of characters in the string.

You can return this string array in any order, but there can be no duplicate elements.

Example:

```Input: s = "abc"
Output:["abc","acb","bac","bca","cab","cba"]
```

Limitations:

```1 <= s Length of <= 8
```
##### Train of thought I
```class Solution {
public:
int n;
set<string> st;
string s;
void dfs(int pos, string cur){
if(pos == n){
st.insert(cur);
return ;
}
for(int i = pos; i < n; ++ i){
swap(s[i], s[pos]);
dfs(pos + 1, cur + s[pos]);
swap(s[i], s[pos]);
}
}
vector<string> permutation(string ss) {
st.clear();
s = ss;
vector<string> ans;
n = s.size();
dfs(0, "");
for(auto str : st)ans.push_back(str);
return ans;
}
};
```

Algorithm idea

Finding permutation is an example of permutation tree model. It mainly uses backtracking algorithm. It is exchanged one by one in the process of ascending and traversing. When it reaches the end point, the result can be obtained by inserting the set to remove duplication. ##### Train of thought II
```class Solution {
public:
vector<string> permutation(string s) {
vector<string> ans;
sort(s.begin(), s.end());
do{
ans.push_back(s);
}while(next_permutation(s.begin(), s.end()));
return ans;
}
};
```

Algorithm idea

Next of C + + standard library_ Permutation can quickly obtain the current maximum arrangement of strings. This function can quickly solve the problem. Posted on Wed, 06 Oct 2021 13:17:19 -0400 by pr0x