#pragma once
#include<string>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
class TreeNode {
public:
int val;
TreeNode *left, *right;
TreeNode(int val)
{
this->val = val;
this->left = this->right = NULL;
}
};
typedef struct TwoStrings {
string lnr, lrn;
TwoStrings(string s1, string s2):lnr(s1),lrn(s2){
}
bool isEmpty() {
return lnr == ""||lrn == "";
}
}TS;
class CreatTree
{
public:
CreatTree(){
}
~CreatTree(){
}
TreeNode* create(TS source) {
if (source.isEmpty()) return NULL;
char rootChar = source.lrn.back();
TreeNode* root = new TreeNode(rootChar);
int strLen = source.lnr.size();
int rootPosInLnr = source.lnr.find(rootChar);
int lPartLine = findPosInLrn(source,rootPosInLnr);
TS lchild(source.lnr.substr(0, rootPosInLnr), source.lrn.substr(0, lPartLine));
TS rchild(source.lnr.substr(rootPosInLnr + 1, strLen - 1 - rootPosInLnr),
source.lrn.substr(lPartLine,strLen-1- lPartLine));
root->left = create(lchild);
root->right = create(rchild);
return root;
}
int findPosInLrn(TS &source, int rootPosInLnr) {
string lLnrStr = source.lnr.substr(0, rootPosInLnr);
string lrn = source.lrn;
int MaxPos = -1;
if (lLnrStr != "") {
for (int i = 0; i < lLnrStr.size(); ++i) {
MaxPos = max(MaxPos, (int)lrn.find(lLnrStr[i]));
}
}
return MaxPos + 1;
}
void preVisit(TreeNode* T) {
if (T == NULL) return;
else {
cout << (char)(T->val) << " ";
preVisit(T->left);
preVisit(T->right);
}
}
void BFS(TreeNode* T) {
queue<TreeNode*> que;
if (T != NULL) {
que.push(T);
while (!que.empty()) {
TreeNode* nowPtr = que.front();
cout <<(char)nowPtr->val << " ";
que.pop();
if (nowPtr->left) {
que.push(nowPtr->left);
}
if (nowPtr->right) {
que.push(nowPtr->right);
}
}
}
}
private:
};
void testForCreatT() {
CreatTree test;
TS str("CBHADE","CHBEDA");
test.BFS(test.create(str));
}
评估:子串复制浪费空间,可以只传入指针,直接访问原来的字符串
#pragma once
#include<string>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
class TreeNode {
public:
char val;
TreeNode *left, *right;
TreeNode(int val)
{
this->val = val;
this->left = this->right = NULL;
}
};
class CreateTree
{
public:
CreateTree(){
}
CreateTree(string last,string in):m_last(last),m_in(in) {
m_root=create(0, in.size() - 1, 0, last.size() - 1);
}
~CreateTree(){
}
TreeNode* create(int in0,int in1,int last0,int last1) {
char rootVal = m_last[last1];
cout << " " << rootVal << endl;
TreeNode* root = new TreeNode(rootVal);
int PosIn = findRootOfIn(rootVal);
int leftLen = PosIn - in0;
if(leftLen > 0)
root->left = create(in0, PosIn - 1, last0, last0 + leftLen - 1);
int rightLen = in1 - PosIn;
if (rightLen > 0)
root->right = create(PosIn + 1, in1, last1 - rightLen, last1 - 1);
return root;
}
int findRootOfIn(char root) {
int pos = -1;
for (int i = 0; i < m_in.size(); i++) {
if (m_in[i] == root)
{
pos = i;
break;
}
}
return pos;
}
void BFS() {
queue<TreeNode*> q;
if (m_root) {
q.push(m_root);
while (!q.empty()) {
TreeNode* now = q.front();
q.pop();
cout << now->val << " ";
if (now->left) q.push(now->left);
if (now->right) q.push(now->right);
}
}
}
private:
string m_last, m_in;
TreeNode* m_root;
};
void testForCreateTree() {
CreateTree test("DEBFCA", "DBEACF");
test.BFS();
}
- 注意下标的合法性
if (rightLen > 0) root->right = create(PosIn + 1, in1, last1 - rightLen, last1 - 1);
推理如下:
L>posIn+1>0,L>in1>0,L>last1 - rightLen>0,L>last1 - 1>0;
此外末尾大于开始,last1-1>=last1-rightLen
可得,rightLen>=1#
- 层次遍历
不断将出队的孩子入队,直到队列为空。
如果数据val是结构体,空间大于四个字节,不妨把指入队,节约空间。