#include<iostream>#include<vector>#include<string>#include<algorithm>\nusing\ namespace\ std;\nstruct\ node{\n\tchar\ val;\n\tnode\ l;\n\tnode\ r;\n\tnode(char\ x)\ :\ val(x),\ l(NULL),\ r(NULL){\n\t}\n};\nnode\ buildtree(string\ s){\n\tnode\ root;\n\tif(s.length()==0){\n\t return\ NULL;\n\t}\n\telse\ if(s.length()==1){\n\t root\ =\ new\ node(s[0]);\n\t root->l=NULL;\n\t root->r=NULL;\n\t return\ root;\n\t}\n\telse{\n\t int\ mid=s.length()/2;\n\t root\ =\ new\ node(' ');\n\t root->l=buildtree(s.substr(0,mid));\n\t root->r=buildtree(s.substr(mid,mid));\n\t if(root->l->val==root->r->val){\n\t root->val=root->r->val;\n\t }\n\t else{\n\t root->val='F';\n\t }\n\t return\ root;\n\t}\n}\nvoid\ post(node\ *root){\n\tif(root!=NULL){\n\t post(root->l);\n\t post(root->r);\n\t cout<val;\n\t}\n}\nint\ main(){\n\tint\ n;\n\tstring\ input;\n\tcin>>n>>input;\n\tnode\ *res;\n\tres=buildtree(input);\n\tpost(res);\n\treturn\ 0;\n\

C++ 代码改进:修复无法输出结果的树构建和后序遍历代码

原文地址: https://www.cveoy.top/t/topic/pAAn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录