struct TNode {
char val;
TNode* left;
TNode* right;
};
void createTree(TNode * &root)
{
char ch;
cin >> ch;
if (ch != '#')
{
root = new TNode;
root->val = ch;
createTree(root->left);
createTree(root->right);
}
else
{
root = NULL;
}
}
void pre(TNode* root)
{
if (root == NULL) return;
cout << root->val;
pre(root->left);
pre(root->right);
}
vector<vector<char>> res;
vector<vector<char>> levelOrder(TNode* root)
{
queue<TNode*> que;
if (root != NULL) que.push(root);
while (!que.empty())
{
int size = que.size();
vector<char> resTmp;
for (int i = 0; i < size; i++)
{
TNode* node = que.front();
que.pop();
resTmp.push_back(node->val);
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
res.push_back(resTmp);
}
return res;
}```
创建二叉树
最新推荐文章于 2024-08-30 16:00:44 发布