#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
class TreeToSequence {
public:
string toSequence(TreeNode* root) {
if(root == NULL) return "";
string resultString = "";
resultString += "(";
string left = toSequence(root->left);
string right = toSequence(root->right);
resultString += left;
resultString += right;
resultString += ")";
return resultString;
}
};
int main()
{
TreeNode *root = new TreeNode(1);
TreeNode *left = new TreeNode(2);
TreeNode *right = new TreeNode(3);
root->left = left;
root->right = right;
TreeToSequence *obj = new TreeToSequence();
string res = obj->toSequence(root);
cout << res << endl;
delete left;
delete right;
delete root;
return 0;
}
二叉树的序列化
最新推荐文章于 2022-09-17 10:30:53 发布