給定層次序列字符串,以’ ,’ 分隔,’#’ 表示無節點,創建二叉樹後以先序方式打印二叉樹。
//
// Created by 91241 on 2018-10-28.
//
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
class TreeNode {
public:
int val;
TreeNode *left, *right;
TreeNode(int val){
this->val = val;
this->left = this->right = NULL;
}
};
// 按層次序列創建二叉樹, 假設沒有負數,'#'表示該節點不存在
TreeNode *createTreeNode_bfs(string str) {
vector<int> v;
char *str_c = new char[str.length() + 1]; // strtok() 只能處理字符數組;
copy(str.begin(), str.end(), str_c);
char *tmp = strtok(str_c, ","); // 分割文本
while(tmp != NULL){
if(*tmp == '#'){
v.push_back(-1);
} else {
v.push_back(atoi(tmp));
}
tmp = strtok(NULL, ",");
}
TreeNode *root = new TreeNode(v[0]), *p;
TreeNode *que[100] = {root}; // 創建隊列
int front = 0, tail = 1; // 首尾指針
vector<int>::iterator it = v.begin() + 1;
while(front != tail){ // 首尾相等表示隊空
if(it == v.end()) break;
if(que[front] == NULL){
front++;
continue;
}
p = que[front];
if(*it == -1) que[tail++] = NULL; // 兩個子節點入隊,-1表示節點不存在
else que[tail++] = new TreeNode(*it);
it++;
if(*it == -1) que[tail++] = NULL;
else que[tail++] = new TreeNode(*it);
it++;
p->left = que[tail - 2];
p->right = que[tail - 1];
front++; // 出隊操作
}
return root;
}
// 輸出先序遍歷
void printTree(TreeNode *root){
if(root != NULL){
cout << root->val << ' ';
printTree(root->left);
printTree(root->right);
}else{
cout << "# ";
}
}
int main() {
TreeNode *root = createTreeNode_bfs("1,2,3,4,#,6,#");
printTree(root);
return 0;
}