二叉树的遍历 & C++代码实现

二叉树的遍历

  • 先序遍历:到达一个节点后,即刻输出该节点的值,并继续遍历其左右子树。
  • 中序遍历:到达一个节点后,将其暂存,遍历完左子树后,再输出该节点的值,然后遍历右子树。
  • 后序遍历:到达一个节点后,将其暂存,遍历完左右子树后,再输出该节点的值。
  • 层次遍历

二叉树的节点类

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为空时,说明根和左子树都遍历完了,该进入右子树了
            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为空时,说明根和左子树都遍历完了,该进入右子树了
		{
			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;

	//先序遍历:1  2  3  4  5  6  7  8
	cout << "递归的先序遍历" << endl;
	pre_traversal_recur(a);
	cout << endl;

	cout << "非递归的先序遍历" << endl;
	pre_traversal(a);
	cout << endl << endl;

	//中序遍历:3  4  2  1  6  5  8  7
	cout << "递归的中序遍历" << endl;
	in_traversal_recur(a);
	cout << endl;

	cout << "非递归的中序遍历" << endl;
	in_traversal(a);
	cout << endl << endl;

	//后序遍历:4  3  2  6  8  7  5  1
	cout << "递归的后序遍历" << endl;
	post_traversal_recur(a);
	cout << endl;

	cout << "非递归的后序遍历" << endl;
	post_traversal(a);
	cout << endl << endl;

	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值