二叉树的遍历
- 先序遍历:到达一个节点后,即刻输出该节点的值,并继续遍历其左右子树。
- 中序遍历:到达一个节点后,将其暂存,遍历完左子树后,再输出该节点的值,然后遍历右子树。
- 后序遍历:到达一个节点后,将其暂存,遍历完左右子树后,再输出该节点的值。
- 层次遍历
二叉树的节点类
struct BinaryTreeNode {
int val;
BinaryTreeNode* leftchild;
BinaryTreeNode* rightchild;
BinaryTreeNode(int x) : val(x), leftchild(NULL), rightchild(NULL) {}
};
先序遍历-非递归
力扣先序遍历题目链接
void pre_traversal(BinaryTreeNode* root) {
stack<BinaryTreeNode*> node_stack;
while (root != nullptr || !node_stack.empty()) {
if (root != nullptr) {
cout << root->val << " ";
node_stack.push(root);
root = root->leftchild;
}
else {
root = node_stack.top();
node_stack.pop();
root = root->rightchild;
}
}
}
中序遍历-非递归
力扣中序遍历题目链接
void in_traversal(BinaryTreeNode* root) {
stack<BinaryTreeNode*> stack_node;
while (root != nullptr || !stack_node.empty()) {
if (root != nullptr) {
stack_node.push(root);
root = root->leftchild;
}
else {
root = stack_node.top();
cout << root->val << " ";
stack_node.pop();
root = root->rightchild;
}
}
}
后序遍历-非递归
力扣后序遍历题目链接
void post_traversal(BinaryTreeNode* root) {
stack<BinaryTreeNode*> stack_node;
BinaryTreeNode* lastvisit = root;
while (root != nullptr || !stack_node.empty()) {
if (root != nullptr) {
stack_node.push(root);
root = root->leftchild;
}
else {
root = stack_node.top();
if (root->rightchild == nullptr || root->rightchild == lastvisit) {
cout << root->val << " ";
stack_node.pop();
lastvisit = root;
root = nullptr;
}
else {
root = root->rightchild;
}
}
}
}
递归和非递归完整版代码
#include<iostream>
#include<stack>
using namespace std;
struct BinaryTreeNode
{
int val;
BinaryTreeNode* leftChild;
BinaryTreeNode* rightChild;
BinaryTreeNode(int x) : val(x), leftChild(NULL), rightChild(NULL) {};
};
void pre_traversal_recur(BinaryTreeNode* root)
{
if (root != NULL)
{
cout << root->val << " ";
pre_traversal_recur(root->leftChild);
pre_traversal_recur(root->rightChild);
}
}
void pre_traversal(BinaryTreeNode* root)
{
stack<BinaryTreeNode*>node_stack;
while (root != nullptr || !node_stack.empty())
{
if (root != nullptr)
{
cout << root->val << " ";
node_stack.push(root);
root = root->leftChild;
}
else
{
root = node_stack.top();
node_stack.pop();
root = root->rightChild;
}
}
}
void in_traversal_recur(BinaryTreeNode* root)
{
if (root != NULL)
{
in_traversal_recur(root->leftChild);
cout << root->val << " ";
in_traversal_recur(root->rightChild);
}
}
void in_traversal(BinaryTreeNode* root)
{
stack<BinaryTreeNode*>node_stack;
while (root != nullptr || !node_stack.empty())
{
if (root != nullptr)
{
node_stack.push(root);
root = root->leftChild;
}
else
{
root = node_stack.top();
cout << root->val << " ";
node_stack.pop();
root = root->rightChild;
}
}
}
void post_traversal_recur(BinaryTreeNode* root)
{
if (root != NULL)
{
post_traversal_recur(root->leftChild);
post_traversal_recur(root->rightChild);
cout << root->val << " ";
}
}
void post_traversal(BinaryTreeNode* root)
{
stack<BinaryTreeNode*>node_stack;
BinaryTreeNode* lastvisit = root;
while (root != nullptr || !node_stack.empty())
{
if (root != nullptr)
{
node_stack.push(root);
root = root->leftChild;
}
else
{
root = node_stack.top();
if (root->rightChild == nullptr || root->rightChild == lastvisit)
{
cout << root->val << " ";
node_stack.pop();
lastvisit = root;
root = nullptr;
}
else
{
root = root->rightChild;
}
}
}
}
int main()
{
BinaryTreeNode* a = new BinaryTreeNode(1);
BinaryTreeNode* b = new BinaryTreeNode(2);
BinaryTreeNode* c = new BinaryTreeNode(3);
BinaryTreeNode* d = new BinaryTreeNode(4);
BinaryTreeNode* e = new BinaryTreeNode(5);
BinaryTreeNode* f = new BinaryTreeNode(6);
BinaryTreeNode* g = new BinaryTreeNode(7);
BinaryTreeNode* h = new BinaryTreeNode(8);
a->leftChild = b;
a->rightChild = e;
b->leftChild = c;
c->rightChild = d;
e->leftChild = f;
e->rightChild = g;
g->leftChild = h;
cout << "递归的先序遍历" << endl;
pre_traversal_recur(a);
cout << endl;
cout << "非递归的先序遍历" << endl;
pre_traversal(a);
cout << endl << endl;
cout << "递归的中序遍历" << endl;
in_traversal_recur(a);
cout << endl;
cout << "非递归的中序遍历" << endl;
in_traversal(a);
cout << endl << endl;
cout << "递归的后序遍历" << endl;
post_traversal_recur(a);
cout << endl;
cout << "非递归的后序遍历" << endl;
post_traversal(a);
cout << endl << endl;
return 0;
}