二叉树:前序,中序,后序,非递归调用C++实现

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

};


    void preorder_print1(TreeNode* root) //非递归调用 前序
    {
        if(!root) return ;
        stack<TreeNode*> temp;

        while(root || !temp.empty())
        {
            if(root)
            {
                cout<<root->val<<" ";
                temp.push(root);
                root=root->left;
            }else
            {
                TreeNode* node=temp.top();
                temp.pop();
                root=node->right;
            }

        }
        cout<<endl;
    }

    void zhongxu_print1(TreeNode* root)   // 非递归调用 中序
    {
        if(!root) return ;
        stack<TreeNode*> temp;
        while(root || !temp.empty())
        {
            while(root)
            {
                temp.push(root);
                root=root->left;
            }
            TreeNode* node=temp.top();
            cout<<node->val<<" ";
            temp.pop();
            root=node->right;
        }
        cout<<endl;
    }
    void houxu_print1(TreeNode* root)  // 非递归调用 后序
    {
        if(!root) return ;
        stack<TreeNode*> temp;
        TreeNode* pre;
        temp.push(root);
        while(!temp.empty())
        {
            TreeNode* curr=temp.top();
            if((!curr->left && !curr->right) || (pre && (curr->left==pre || curr->right==pre)))
            {
                cout<<curr->val<<" ";
                temp.pop();
                pre=curr;
            }else
            {
                if(curr->right)
                {
                    temp.push(curr->right);
                }
                if(curr->left)
                {
                    temp.push(curr->left);
                }

            }
        }
        cout<<endl;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C++实现二叉树前序中序后序遍历的示例代码: ```cpp #include <iostream> using namespace std; // 定义二叉树结点 struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; // 构建二叉树 TreeNode* buildTree(int pre[], int in[], int preStart, int preEnd, int inStart, int inEnd) { if (preStart > preEnd || inStart > inEnd) { return nullptr; } int rootVal = pre[preStart]; int rootIndex = 0; for (int i = inStart; i <= inEnd; i++) { if (in[i] == rootVal) { rootIndex = i; break; } } int leftLen = rootIndex - inStart; TreeNode* root = new TreeNode(rootVal); root->left = buildTree(pre, in, preStart + 1, preStart + leftLen, inStart, rootIndex - 1); root->right = buildTree(pre, in, preStart + leftLen + 1, preEnd, rootIndex + 1, inEnd); return root; } // 前序遍历 void preOrder(TreeNode* root) { if (root == nullptr) { return; } cout << root->val << " "; preOrder(root->left); preOrder(root->right); } // 中序遍历 void inOrder(TreeNode* root) { if (root == nullptr) { return; } inOrder(root->left); cout << root->val << " "; inOrder(root->right); } // 后序遍历 void postOrder(TreeNode* root) { if (root == nullptr) { return; } postOrder(root->left); postOrder(root->right); cout << root->val << " "; } int main() { int pre[] = {1, 2, 4, 5, 3, 6, 7}; int in[] = {4, 2, 5, 1, 6, 3, 7}; TreeNode* root = buildTree(pre, in, 0, 6, 0, 6); cout << "前序遍历结果:"; preOrder(root); cout << endl; cout << "中序遍历结果:"; inOrder(root); cout << endl; cout << "后序遍历结果:"; postOrder(root); cout << endl; return 0; } ``` 在上面的代码中,我们首先定义了二叉树结点结构体`TreeNode`,然后实现了一个`buildTree`函数,该函数用于根据前序遍历数组和中序遍历数组构建二叉树。接着,我们分别实现前序遍历、中序遍历和后序遍历的函数,最后在主函数中调用这些函数进行遍历。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值