Skip to content

Commit 6c40fc8

Browse files
authored
Add files via upload
1 parent dd8b9d0 commit 6c40fc8

File tree

2 files changed

+328
-0
lines changed

2 files changed

+328
-0
lines changed

0000.test_project/Tree_to_String.h

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
#ifndef TREE_TO_STRING
2+
#define TREE_TO_STRING
3+
4+
#include <string>
5+
#include <vector>
6+
#include <queue>
7+
#include <stack>
8+
#include <iostream>
9+
using namespace std;
10+
11+
struct TreeNode {
12+
int val;
13+
TreeNode *left;
14+
TreeNode *right;
15+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
16+
};
17+
18+
class Tree_String {
19+
public:
20+
21+
// Encodes a tree to a single string.
22+
string serialize(TreeNode* root) {
23+
vector<string> v_ans;
24+
queue<TreeNode*> helper;
25+
helper.push(root);
26+
while (!helper.empty())
27+
{
28+
TreeNode *p=helper.front();
29+
helper.pop();
30+
if (p!=NULL)
31+
{
32+
v_ans.push_back(to_string(p->val));
33+
helper.push(p->left);
34+
helper.push(p->right);
35+
}
36+
//else if (p==NULL)
37+
else
38+
{
39+
v_ans.push_back("null");
40+
}
41+
}
42+
while (v_ans.size()>1 && v_ans.back()=="null")
43+
v_ans.pop_back();
44+
string ans;
45+
for (string &i:v_ans)
46+
ans+=i+',';
47+
ans.pop_back();
48+
return ans;
49+
}
50+
51+
// Decodes your encoded data to tree.
52+
TreeNode* deserialize(string data) {
53+
vector<string> v_data;
54+
int data_size=data.size();
55+
string letter;
56+
for (int i=0;i<=data_size;++i)
57+
{
58+
if (i==data_size || data.at(i)==',')
59+
{
60+
v_data.push_back(letter);
61+
letter.clear();
62+
}
63+
else
64+
letter+=data.at(i);
65+
}
66+
if (v_data.at(0)=="null")
67+
return NULL;
68+
int v_data_size=v_data.size();
69+
TreeNode * root=new TreeNode(stoi(v_data.at(0)));
70+
queue<TreeNode *> helper;
71+
helper.push(root);
72+
helper.push(root);
73+
int index=1;
74+
bool left_child=true;
75+
while (index<v_data_size)
76+
{
77+
TreeNode * p=helper.front();
78+
helper.pop();
79+
if (left_child)
80+
{
81+
if (v_data.at(index)!="null")
82+
{
83+
p->left=new TreeNode(stoi(v_data.at(index)));
84+
helper.push(p->left);
85+
helper.push(p->left);
86+
}
87+
}
88+
else
89+
{
90+
if (v_data.at(index)!="null")
91+
{
92+
p->right=new TreeNode(stoi(v_data.at(index)));
93+
helper.push(p->right);
94+
helper.push(p->right);
95+
}
96+
}
97+
++index;
98+
left_child=!left_child;
99+
}
100+
return root;
101+
}
102+
};
103+
104+
class show_tree {
105+
public:
106+
vector<vector<int>> levelOrder(TreeNode* root) {
107+
queue<TreeNode*> queue_root;
108+
if (root!=NULL)
109+
queue_root.push(root);
110+
vector<vector<int>> answer;
111+
int index=-1,loop_times;
112+
TreeNode* p;
113+
while (!queue_root.empty())
114+
{
115+
answer.push_back({});
116+
++index;
117+
loop_times=queue_root.size();
118+
for (int i=0;i<loop_times;++i)
119+
{
120+
p=queue_root.front();
121+
answer.at(index).push_back(int(p->val));
122+
if (p->left!=NULL)
123+
queue_root.push(p->left);
124+
if (p->right!=NULL)
125+
queue_root.push(p->right);
126+
queue_root.pop();
127+
}
128+
}
129+
return answer;
130+
}
131+
132+
vector<int> inorderTraversal(TreeNode* root) {
133+
stack<TreeNode*> stack_roots;
134+
vector<int> answer;
135+
while(root!=NULL || !stack_roots.empty())
136+
if (root!=NULL)
137+
{
138+
stack_roots.push(root);
139+
root=root->left;
140+
}
141+
else
142+
{
143+
root=stack_roots.top();
144+
stack_roots.pop();
145+
answer.push_back(int(root->val));
146+
root=root->right;
147+
}
148+
return answer;
149+
}
150+
151+
vector<int> preorderTraversal(TreeNode* root) {
152+
stack<TreeNode*> root_path;
153+
vector<int> answer({});
154+
while (root!=NULL || !root_path.empty())
155+
if (root!=NULL)
156+
{
157+
answer.push_back(int(root->val));
158+
root_path.push(root);
159+
root=root->left;
160+
}
161+
else
162+
{
163+
root=root_path.top();
164+
root_path.pop();
165+
root=root->right;
166+
}
167+
return answer;
168+
}
169+
170+
vector<int> postorderTraversal(TreeNode* root) {
171+
vector<int> answer;
172+
stack<TreeNode*> root_path;
173+
TreeNode* past=NULL;
174+
while (root!=NULL || !root_path.empty())
175+
if (root!=NULL)
176+
{
177+
root_path.push(root);
178+
root=root->left;
179+
}
180+
else
181+
{
182+
root=root_path.top();
183+
if (root->right==NULL || root->right==past)
184+
{
185+
answer.push_back(root->val);
186+
root_path.pop();
187+
past=root;
188+
root=NULL;
189+
}
190+
else
191+
root=root->right;
192+
}
193+
return answer;
194+
}
195+
};
196+
#endif

0000.test_project/show.h

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#ifndef SHOW
2+
#define SHOW
3+
4+
#include <string>
5+
#include <vector>
6+
#include <list>
7+
#include <unordered_map>
8+
#include <map>
9+
#include <unordered_set>
10+
#include <set>
11+
#include <iostream>
12+
using namespace std;
13+
14+
struct ListNode
15+
{
16+
int val;
17+
ListNode *next;
18+
ListNode(int _val) : val(_val) { next = NULL; }
19+
};
20+
21+
void show(vector<vector<bool>> answer)
22+
{
23+
cout << boolalpha;
24+
for (vector<bool> &i : answer)
25+
{
26+
for (bool j : i)
27+
cout << j << " ";
28+
cout << endl;
29+
}
30+
cout << "---------------------------" << endl;
31+
cout << noboolalpha;
32+
}
33+
template <typename T>
34+
void show(vector<vector<T>> answer)
35+
{
36+
int answer_size = answer.size();
37+
int single_answer_size;
38+
cout << "-------" << endl;
39+
cout << "size=" << answer.size() << endl
40+
<< "~~~~" << endl;
41+
for (int i = 0; i < answer_size; ++i)
42+
{
43+
single_answer_size = answer.at(i).size();
44+
for (int j = 0; j < single_answer_size; ++j)
45+
cout << answer.at(i).at(j) << " ";
46+
cout << endl;
47+
}
48+
cout << "-------" << endl;
49+
}
50+
template <typename T>
51+
void show(vector<T> answer)
52+
{
53+
cout << "----" << endl;
54+
int answer_size = answer.size();
55+
cout << "size=" << answer.size() << endl
56+
<< "~~~~" << endl;
57+
for (int i = 0; i < answer_size; ++i)
58+
cout << answer.at(i) << endl;
59+
cout << "----" << endl;
60+
}
61+
void show(vector<pair<int,int>> answer)
62+
{
63+
cout << "----" << endl;
64+
int answer_size = answer.size();
65+
cout << "size=" << answer.size() << endl
66+
<< "~~~~" << endl;
67+
for (int i = 0; i < answer_size; ++i)
68+
cout << answer.at(i).first << ' ' << answer.at(i).second << endl;
69+
cout << "----" << endl;
70+
}
71+
template <typename T>
72+
void show(list<T> answer)
73+
{
74+
cout << "----" << endl;
75+
cout << "size=" << answer.size() << endl
76+
<< "~~~~" << endl;
77+
for (auto p=answer.begin();p!=answer.end();++p)
78+
cout << *p << endl;
79+
cout << "----" << endl;
80+
}
81+
void show(ListNode * p)
82+
{
83+
cout << "----" << endl;
84+
while (p!=NULL)
85+
{
86+
cout << p->val << endl;
87+
p=p->next;
88+
}
89+
cout << "----" << endl;
90+
}
91+
template <typename K,typename V>
92+
void show(unordered_map<K,V> answer)
93+
{
94+
cout << "----" << endl;
95+
cout << "size=" << answer.size() << endl
96+
<< "~~~~" << endl;
97+
for (auto p=answer.begin();p!=answer.end();++p)
98+
cout << p->first << ' ' << p->second << endl;
99+
cout << "----" << endl;
100+
}
101+
template <typename K,typename V>
102+
void show(map<K,V> answer)
103+
{
104+
cout << "----" << endl;
105+
cout << "size=" << answer.size() << endl
106+
<< "~~~~" << endl;
107+
for (auto p=answer.begin();p!=answer.end();++p)
108+
cout << p->first << ' ' << p->second << endl;
109+
cout << "----" << endl;
110+
}
111+
template <typename T>
112+
void show(unordered_set<T> answer)
113+
{
114+
cout << "----" << endl;
115+
cout << "size=" << answer.size() << endl
116+
<< "~~~~" << endl;
117+
for (auto p=answer.begin();p!=answer.end();++p)
118+
cout << *p << endl;
119+
cout << "----" << endl;
120+
}
121+
template <typename T>
122+
void show(set<T> answer)
123+
{
124+
cout << "----" << endl;
125+
cout << "size=" << answer.size() << endl
126+
<< "~~~~" << endl;
127+
for (auto p=answer.begin();p!=answer.end();++p)
128+
cout << *p << endl;
129+
cout << "----" << endl;
130+
}
131+
132+
#endif

0 commit comments

Comments
 (0)