算法与数据结构基础(三)之遍历二叉树

递归版

前序遍历(递归)

void PreOrder(Node* pParent)
{
	if (pParent == nullptr)
		return;
	cout << pParent->value << " ";
	PreOrder(pParent->left);
	PreOrder(pParent->right);
}

中序遍历(递归)

void InOrder(Node* pParent)
{
	if (pParent == nullptr)
		return;
	InOrder(pParent->left);
	cout << pParent->value << " ";
	InOrder(pParent->right);
}
// Recursion
class Solution {
    public:
        vector<int> inorderTraversal (TreeNode *root){
            vector<int> res;
            inorder(root, res);
            return res;
        }
    void inorder(TreeNode *root, vector<int> &res){
        if(!root) return;
        if(root->left) inorder(root->left, res);
        res.push_back(root->val);
        if(root->right) inorder(root->right,res);
    } 
};

后序遍历(递归)

void PosOrder(Node* pParent)
{
	if (pParent == nullptr)
		return;
	PosOrder(pParent->left);
	PosOrder(pParent->right);
	cout << pParent->value << " ";
}

测试代码

#include <iostream>

using namespace std;

// 二叉树节点
struct Node
{
	int value;
	struct Node* left;
	struct Node* right;
	Node(int data) : value(data), left(nullptr), right(nullptr) {};
};

// 先序遍历
void PreOrder(Node* pParent)
{
	if (pParent == nullptr)
		return;
	cout << pParent->value << " ";
	PreOrder(pParent->left);
	PreOrder(pParent->right);
}

// 中序遍历
void InOrder(Node* pParent)
{
	if (pParent == nullptr)
		return;
	InOrder(pParent->left);
	cout << pParent->value << " ";
	InOrder(pParent->right);
}

// 后序遍历
void PosOrder(Node* pParent)
{
	if (pParent == nullptr)
		return;
	PosOrder(pParent->left);
	PosOrder(pParent->right);
	cout << pParent->value << " ";
}
void ConnectTreeNodes(Node* pParent, Node* pLeft, Node* pRight)
{
    if(pParent != nullptr)
    {
        pParent->left = pLeft;
        pParent->right = pRight;
    }
}
int main()
{
	Node* pNode1 = new Node(1);
	Node* pNode2 = new Node(2);
	Node* pNode3 = new Node(3);
	Node* pNode4 = new Node(4);
	Node* pNode5 = new Node(5);
	Node* pNode6 = new Node(6);
	Node* pNode7 = new Node(7);

	ConnectTreeNodes(pNode1, pNode2, pNode3);
    ConnectTreeNodes(pNode2, pNode4, pNode5);
    ConnectTreeNodes(pNode3, nullptr, pNode6);
    ConnectTreeNodes(pNode5, pNode7, nullptr);

	PreOrder(pNode1);
	cout << endl;
	InOrder(pNode1);
	cout << endl;
	PosOrder(pNode1);
	cout << endl;
	delete pNode1;
	delete pNode2;
	delete pNode3;
	delete pNode4;
	delete pNode5;
	delete pNode6;
	delete pNode7;
	system("pause");
	return 0;
}

非递归版

前序遍历(非递归)

void PreOrder(Node* pParent)
{
	if (pParent)
	{
		stack<Node*> stack;
		stack.push(pParent);
		Node* cur;
		while (!stack.empty())
		{
			cur = stack.top();
			stack.pop();
			cout << cur->value << " ";
			// 右孩子先进栈,后出
			if (cur->right)
				stack.push(cur->right);
			if (cur->left)
				stack.push(cur->left);
		}
	}
}

中序遍历(非递归)

void InOrder(Node* pParent)
{
	if (pParent)
	{
		stack<Node*> stack;
		Node* cur = pParent;
		while (!stack.empty() || cur != nullptr)
		{
			while (cur != nullptr)
			{
				stack.push(cur);
				cur = cur->left;
			}

			cur = stack.top();
			stack.pop();
			cout << cur->value << " ";
			cur = cur->right;
		}
	}
}

后序遍历(非递归)

void PosOrder(Node* pParent)
{
	if (pParent)
	{
		stack<Node*> s1, s2;
		s1.push(pParent);
		Node* cur;
		while (!s1.empty())
		{
			cur = s1.top();
			s1.pop();
			s2.push(cur);
			if (cur->left)
				s1.push(cur->left);
			if (cur->right)
				s1.push(cur->right);
		}
		while (!s2.empty())
		{
			cout << s2.top()->value << " ";
			s2.pop();
		}
	}
}

测试代码

#include <iostream>
#include <stack>

using namespace std;

// 二叉树节点
struct Node
{
	int value;
	struct Node* left;
	struct Node* right;
	Node(int data) : value(data), left(nullptr), right(nullptr) {};
};

// 先序遍历
void PreOrder(Node* pParent)
{
	if (pParent)
	{
		stack<Node*> stack;
		stack.push(pParent);
		Node* cur;
		while (!stack.empty())
		{
			cur = stack.top();
			stack.pop();
			cout << cur->value << " ";
			// 右孩子先进栈,后出
			if (cur->right)
				stack.push(cur->right);
			if (cur->left)
				stack.push(cur->left);
		}
	}
}


// 中序遍历
void InOrder(Node* pParent)
{
	if (pParent)
	{
		stack<Node*> stack;
		Node* cur = pParent;
		while (!stack.empty() || cur != nullptr)
		{
			while (cur != nullptr)
			{
				stack.push(cur);
				cur = cur->left;
			}

			cur = stack.top();
			stack.pop();
			cout << cur->value << " ";
			cur = cur->right;
		}
	}
}
// 后序遍历
void PosOrder(Node* pParent)
{
	if (pParent)
	{
		stack<Node*> s1, s2;
		s1.push(pParent);
		Node* cur;
		while (!s1.empty())
		{
			cur = s1.top();
			s1.pop();
			s2.push(cur);
			if (cur->left)
				s1.push(cur->left);
			if (cur->right)
				s1.push(cur->right);
		}
		while (!s2.empty())
		{
			cout << s2.top()->value << " ";
			s2.pop();
		}
	}
}
void ConnectTreeNodes(Node* pParent, Node* pLeft, Node* pRight)
{
    if(pParent != nullptr)
    {
        pParent->left = pLeft;
        pParent->right = pRight;
    }
}
int main()
{
	Node* pNode1 = new Node(1);
	Node* pNode2 = new Node(2);
	Node* pNode3 = new Node(3);
	Node* pNode4 = new Node(4);
	Node* pNode5 = new Node(5);
	Node* pNode6 = new Node(6);
	Node* pNode7 = new Node(7);

	ConnectTreeNodes(pNode1, pNode2, pNode3);
    ConnectTreeNodes(pNode2, pNode4, pNode5);
    ConnectTreeNodes(pNode3, nullptr, pNode6);
    ConnectTreeNodes(pNode5, pNode7, nullptr);

	PreOrder(pNode1);
	cout << endl;
	InOrder(pNode1);
	cout << endl;
	PosOrder(pNode1);
	cout << endl;
	delete pNode1;
	delete pNode2;
	delete pNode3;
	delete pNode4;
	delete pNode5;
	delete pNode6;
	delete pNode7;
	system("pause");
	return 0;
}

参考文章:https://blog.csdn.net/silence1772/article/details/83623336

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知行SUN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值