文章目录
一、二叉树的存储结构
typedef struct BTNode{
Elemtype data;
struct BTNode *lchild, *rchild;
}BTNode;
二、二叉树的深度优先遍历
1. 先序遍历算法
(1)递归算法
void preorder(BTNode* root)
{
if (!root) return;
visit(root);
preorder(root->lchild);
preorder(root->rchild);
}
(2)非递归算法
a. 使用stl
void preoder(BTNode* root)
{
if (!root) return;
stack<BTNode*> s;
s.push(root);
while (s.size())
{
auto t = s.top();
s.pop();
visit(t);
if (t->rchild) s.push(t->rchild);
if (t->lchild) s.push(t->lchild);
}
}
b. 不使用stl
void preoder(BTNode* root)
{
if (!root) return;
BTNode* stack[MaxSize];
int top = -1;
stack[++ top] = root;
while (top != -1)
{
auto t = stack[top];
top --;
visit(t);
if (t->rchild) stack[++ top] = t->rchild;
if (t->lchild) stack[++ top] = t->lchild;
}
}
2. 中序遍历算法
(1)递归算法
void inorder(BTNode* root)
{
if (!root) return;
inorder(root->lchild);
visit(root);
inorder(root->rchild);
}
(2)非递归算法
a. 使用stl
void inorder(BTNode* root)
{
stack<BTNode*> s;
auto p = root;
while (p || s.size())
{
if (p)
{
s.push(p);
p = p->lchild;
}
else
{
p = s.top();
s.pop();
visit(p);
p = p->rchild;
}
}
}
b. 不使用stl
void inorder(BTNode* root)
{
if (!root) return;
BTNode* stack[MaxSize];
int top = -1;
auto p = root;
while (p || top != -1)
{
if (p)
{
stack[++ top] = p;
p = p->lchild;
}
else
{
p = stack[top];
top --;
visit(p);
p = p->rchild;
}
}
}
3. 后序遍历算法
(1)递归算法
void postorder(BTNode* root)
{
if (!root) return;
postorder(root->lchild);
postorder(root->rchild);
visit(root);
}
(2)非递归算法
a. 使用stl
void postorder(BTNode* root)
{
if (!root) return;
stack<BTNode*> s;
// p跟中序非递归算法中作用相同,遍历 + 栈顶指针
// r的作用是标记最近访问过的结点
BTNode *p = root, *r = NULL;
while (p || s.size())
{
if (p) // 一路向左,不断入栈,同中序遍历
{
s.push(p);
p = p->lchild;
}
else
{
// 当前p指向空,表示上个入栈的结点没有左孩子了,那就返回去开始找右孩子
// 即让p指向上个入栈的结点,即栈顶元素
// p有右孩子且右孩子没被访问过,p指向其右孩子,停
// 反之,p的右孩子被访问过/不存在,那就直接给p扔出去访问
p = s.top();
if (p->rchild && p->rchild != r)
p = p->rchild;
// 下面两个语句,加与不加均可
// s.push(p);
// p = p->lchild;
else
{
s.pop();
visit(p);
r = p;
p = NULL;
}
}
}
}
b. 不使用stl
void postorder(BTNode* root)
{
if (!root) return;
BTNode* stack[MaxSize];
int top = -1;
BTNode *p = root, *r = NULL;
while (p || top != -1)
{
if (p)
{
stack[++ top] = p;
p = p->lchild;
}
else
{
p = stack[top];
if (p->rchild && p->rchild != r)
p = p->rchild;
else
{
top --;
visit(p);
r = p;
p = NULL;
}
}
}
}
三、二叉树的宽度优先遍历
题目链接
a. 使用stl
不涉及单层:
void levelorder(BTNode* root)
{
if (!root) return;
queue<BTNode*> q;
q.push(root);
while (q.size())
{
auto t = q.front();
q.pop();
visit(t);
if (t->lchild) q.push(t->lchild);
if (t->rchild) q.push(t->rchild);
}
}
涉及到单层(链接题目答案):
vector<vector<int>> res;
vector<vector<int> > levelorder(BTNode* root)
{
if (!root) return res;
queue<BTNode*> q;
q.push(root);
while (q.size())
{
vector<int> re;
queue<BTNode*> tmp;
while (q.size())
{
auto t = q.front();
q.pop();
re.push_back(t->val);
if (t->lchild) tmp.push(t->lchild);
if (t->rchild) tmp.push(t->rchild);
}
q = tmp;
res.push_back(re);
}
return res;
}
b. 不使用stl
void levelorder(BTNode* root) {
if (!root) return;
BTNode* queue[MaxSize];
int front = rear = 0;
queue[0] = root;
while (front <= rear)
{
auto t = queue[front];
front ++;
visit(t);
if (t->lchild) queue[++ rear] = t->lchild;
if (t->rchild) queue[++ rear] = t->rchild;
}
}