5.1.1—二叉树的遍历—Binary Tree Preorder Traversal

这篇博客介绍了如何实现二叉树的前序遍历,提供了递归和迭代两种方法。递归版本直接从根节点开始,先输出节点值,再遍历左子树和右子树。迭代版本使用栈来辅助,先将根节点压栈,然后不断弹出节点并将其右子节点和左子节点压栈,直到栈为空。示例中展示了如何创建一棵二叉树,并进行了前序遍历的打印。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述
Given a binary tree, return the preorder traversal of its nodes’ values.
For example: Given binary tree {1,#,2,3},
1
 \
 2
/
3
return [1,2,3].
Note: Recursive solution is trivial, could you do it iteratively?


#pragma once
#include<iostream>
struct BinaryTreeNode
{
	int                    m_nValue;
	BinaryTreeNode*        m_pLeft;
	BinaryTreeNode*        m_pRight;
};

BinaryTreeNode* CreateBinaryTreeNode(int value);
void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight);
void PrintTreeNode(BinaryTreeNode* pNode);
void PrintTree(BinaryTreeNode* pRoot);
void DestroyTree(BinaryTreeNode* pRoot);

#include "BinaryTree.h"

BinaryTreeNode* CreateBinaryTreeNode(int value)
{
	BinaryTreeNode* pNode = new BinaryTreeNode();
	pNode->m_nValue = value;
	pNode->m_pLeft = NULL;
	pNode->m_pRight = NULL;
	return pNode;
}

void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
{
	if (pParent != NULL)
	{
		pParent->m_pLeft = pLeft;
		pParent->m_pRight = pRight;
	}
}

void PrintTreeNode(BinaryTreeNode* pNode)
{
	if (pNode != NULL)
	{
		printf("value of this node is: %d\n", pNode->m_nValue);

		if (pNode->m_pLeft != NULL)
			printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue);
		else
			printf("left child is null.\n");

		if (pNode->m_pRight != NULL)
			printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue);
		else
			printf("right child is null.\n");
	}
	else
	{
		printf("this node is null.\n");
	}

	printf("\n");
}

void PrintTree(BinaryTreeNode* pRoot)
{
	PrintTreeNode(pRoot); 

	if (pRoot != NULL)
	{
		if (pRoot->m_pLeft != NULL)
			PrintTree(pRoot->m_pLeft);

		if (pRoot->m_pRight != NULL)
			PrintTree(pRoot->m_pRight);
	}
}

void DestroyTree(BinaryTreeNode* pRoot)
{
	if (pRoot != NULL)
	{
		BinaryTreeNode* pLeft = pRoot->m_pLeft;
		BinaryTreeNode* pRight = pRoot->m_pRight;

		delete pRoot;
		pRoot = NULL;

		DestroyTree(pLeft);
		DestroyTree(pRight);
	}
}


#include "BinaryTree.h"
#include <stack>
#include<vector>
using namespace std;
//===二叉树的前序遍历,递归版本
void PreorderTraversal(BinaryTreeNode *proot)
{
	if (proot)
	{
		cout << proot->m_nValue << " ";
		if (proot->m_pLeft)
			PreorderTraversal(proot->m_pLeft);
		if (proot->m_pRight)
			PreorderTraversal(proot->m_pRight);
	}
}
//===二叉树的前序遍历,迭代版本
void PreorderTraversal2(BinaryTreeNode *proot)
{
	stack<BinaryTreeNode*> temp;
	vector<int> cahe;
	temp.push(proot);
	while (!temp.empty())
	{
		BinaryTreeNode *top = temp.top();
		temp.pop();
		if (top)
		{
			cahe.push_back(top->m_nValue);
			temp.push(top->m_pRight);
			temp.push(top->m_pLeft);
		}
	}
	//======
	for (int i = 0; i < cahe.size(); i++)
		cout << cahe[i] << " ";
	cout << endl;
}
// ====================测试代码====================
//            8
//        6      10
//       5 7    9  11
int main()
{
	BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
	BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
	BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
	BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
	BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
	BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);
	BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);

	ConnectTreeNodes(pNode8, pNode6, pNode10);
	ConnectTreeNodes(pNode6, pNode5, pNode7);
	ConnectTreeNodes(pNode10, pNode9, pNode11);
	//===
	//PrintTree(pNode8);
	//===
	PreorderTraversal(pNode8);
	cout << endl;
	//===
	PreorderTraversal2(pNode8);

	//==
	DestroyTree(pNode8);
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值