C++ | 二叉树前序、中序、后序遍历的递归和非递归实现 +层序遍历+深度优先遍历

本文详细介绍了二叉树的前序、中序、后序遍历的递归和非递归实现,包括先访问根节点、左子树和右子树的逻辑。此外,还探讨了层序遍历和深度优先遍历这两种不同的遍历策略,是学习二叉树遍历的重要资源。

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

二叉树的遍历是学习二叉树最基本却极为重要的一环。当你熟练掌握二叉树的遍历之后,你会发现很多题目都是建立在遍历的基础上来解决的。本文章就是为了盘点一下各种遍历算法的原理和实现。

前序遍历

前序遍历也叫先序遍历(preorder),整个操作过程比较简单,先访问根结点,在访问左子树,左子树访问完之后访问右子树。

  1. 若二叉树为空,则什么也不做
  2. 否则:
    2.1 访问根结点
    2.2. 先序遍历左子树
    2.3. 先序遍历右子树

递归实现

void preOrder(BinTree* BT){
   
    if(BT){
   
        printf("%d ", BT->data);
        preOrder(BT->left);
        preOrder(BT->right);
    }
}

非递归实现

void preOrder(TreeNode* root){
   
    if(root == NULL) return;
    stack<TreeNode*> S;    // 初始化栈
    TreeNode* node = root; // 声明遍历指针
    while(node || !S.empty()){
   
    	if(node != NULL)}{
     // 不断访问树的根节点,并且存储左子树(若存在的话)
       		 cout << node->val << " ";   
 			 S.push(node);
        	 node = node->left;
    	}
    	else{
    // 遇到空指针,访问栈顶指针指向的右子树结点
        	node = S.top()->right;
好的,以下是C++数据结构中树的二叉树前序、中后序遍历递归非递归代码,包括输入与输出。 假设二叉树的节点定义如下: ``` struct Node { int val; Node* left; Node* right; Node(int x) : val(x), left(NULL), right(NULL) {} }; ``` 前序遍历递归版本: ``` void preorderTraversal(Node* root) { if (!root) return; cout << root->val << " "; preorderTraversal(root->left); preorderTraversal(root->right); } ``` 非递归版本: ``` void preorderTraversal(Node* root) { stack<Node*> s; s.push(root); while (!s.empty()) { Node* node = s.top(); s.pop(); if (!node) continue; cout << node->val << " "; s.push(node->right); s.push(node->left); } } ``` 中序遍历递归版本: ``` void inorderTraversal(Node* root) { if (!root) return; inorderTraversal(root->left); cout << root->val << " "; inorderTraversal(root->right); } ``` 非递归版本: ``` void inorderTraversal(Node* root) { stack<Node*> s; Node* node = root; while (node || !s.empty()) { while (node) { s.push(node); node = node->left; } node = s.top(); s.pop(); cout << node->val << " "; node = node->right; } } ``` 后序遍历递归版本: ``` void postorderTraversal(Node* root) { if (!root) return; postorderTraversal(root->left); postorderTraversal(root->right); cout << root->val << " "; } ``` 非递归版本: ``` void postorderTraversal(Node* root) { stack<Node*> s; Node* node = root; Node* last = NULL; while (node || !s.empty()) { while (node) { s.push(node); node = node->left; } node = s.top(); if (!node->right || node->right == last) { cout << node->val << " "; s.pop(); last = node; node = NULL; } else { node = node->right; } } } ``` 输入与输出: ``` int main() { Node* root = new Node(1); root->left = new Node(2); root->right = new Node(3); root->left->left = new Node(4); root->left->right = new Node(5); root->right->left = new Node(6); root->right->right = new Node(7); cout << "前序遍历(递归): "; preorderTraversal(root); cout << endl; cout << "前序遍历(非递归): "; preorderTraversal(root); cout << endl; cout << "中序遍历(递归): "; inorderTraversal(root); cout << endl; cout << "中序遍历(非递归): "; inorderTraversal(root); cout << endl; cout << "后序遍历(递归): "; postorderTraversal(root); cout << endl; cout << "后序遍历(非递归): "; postorderTraversal(root); cout << endl; return 0; } ``` 输出结果: ``` 前序遍历(递归): 1 2 4 5 3 6 7 前序遍历(非递归): 1 2 4 5 3 6 7 中序遍历(递归): 4 2 5 1 6 3 7 中序遍历(非递归): 4 2 5 1 6 3 7 后序遍历(递归): 4 5 2 6 7 3 1 后序遍历(非递归): 4 5 2 6 7 3 1 ```
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值