【数据结构】C++对二叉树进行中序遍历(递归、迭代及Morris)

学习目标:使用c++对二叉树进行递归、迭代以及Morris的便利

问题

在这里插入图片描述

递归

二叉树的中序遍历:按照访问左子树——根节点——右子树的方式遍历这棵树,而在访问左子树或者右子树的时候我们按照同样的方式遍历,直到遍历完整棵树。

思路:
在这里插入图片描述

迭代

请添加图片描述

Morris

在这里插入图片描述
请添加图片描述

代码实现

#include <iostream>
#include <vector>
#include <stack>

using namespace std;
using std::vector;


 struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode() : val(0), left(nullptr), right(nullptr) {}
      TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
      TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  };
 

// 递归
class DiGui {
public:
    void inorder(TreeNode* root, vector<int>& res) {
        if (!root) {
            return;
        }

        inorder(root->left, res);
        cout << root->val << " ";
        res.push_back(root->val);  // 在 vector 类中作用为在 vector 尾部加入一个数据。 string中也有这个函数,作用是字符串之后插入一个字符。
        inorder(root->right, res);
    }
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        inorder(root, res);
        return res;
    }
};

// 迭代
class DieDai {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> stk;
        cout << "\n" << endl;
        while (root != nullptr || !stk.empty()) {
            while (root != nullptr) {
                stk.push(root);
                root = root->left;
            }
            root = stk.top();
            stk.pop();
            cout << root->val << " ";
            res.push_back(root->val);
            root = root->right;
        }
        return res;
    }
};

// Morris算法
class Morris {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        TreeNode* predecessor = nullptr;
        cout << "\n" << endl;

        while (root != nullptr) {
            if (root->left != nullptr) {
                // predecessor 节点就是当前 root 节点向左走一步,然后一直向右走至无法走为止
                predecessor = root->left;
                while (predecessor->right != nullptr && predecessor->right != root) {
                    predecessor = predecessor->right;
                }

                // 让 predecessor 的右指针指向 root,继续遍历左子树
                if (predecessor->right == nullptr) {
                    predecessor->right = root;
                    root = root->left;
                }
                // 说明左子树已经访问完了,我们需要断开链接
                else {
                    cout << root->val << " ";
                    res.push_back(root->val);
                    predecessor->right = nullptr;
                    root = root->right;
                }
            }
            // 如果没有左孩子,则直接访问右孩子
            else {
                cout << root->val << " ";
                res.push_back(root->val);
                root = root->right;
            }
        }
        return res;
    }
};




int main()
{
    TreeNode* A = new TreeNode(1);
   /* TreeNode* B = new TreeNode(NULL);*/     // 当子树为NULL时,打印出的值为0
    TreeNode* C = new TreeNode(2);
    TreeNode* D = new TreeNode(3);
  /*  A->left = B;*/
    A->right = C;  
    C->left = D;
    DiGui digui;                             // 执行递归
    vector<int> V1 = digui.inorderTraversal(A);
     
    DieDai diedai;                           // 执行迭代
    vector<int> V2 = diedai.inorderTraversal(A);
    
    Morris morris;                           // 执行morris
    vector<int> V3 = diedai.inorderTraversal(A);
}

运行结果

在这里插入图片描述

参考

https://leetcode.cn/problems/binary-tree-inorder-traversal/solution/er-cha-shu-de-zhong-xu-bian-li-by-leetcode-solutio/

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

只搬烫手的砖

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

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

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

打赏作者

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

抵扣说明:

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

余额充值