zoukankan      html  css  js  c++  java
  • 树的非递归前序、中序、后序遍历

      

    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <vector>
    #include <stack>
    using namespace std;
    
     struct TreeNode {
         int val;
         TreeNode *left;
         TreeNode *right;
         TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     };
    
     class Solution {
     public:
         void preOrder(TreeNode *root) 
         {
             stack<TreeNode *>temp;
             TreeNode *current = root;
             while (current || !temp.empty())
             {
                 while (current)
                 {
                     temp.push(current);
                     printf("%d ", current->val);
                     current = current->left;
                 }
                 if (!temp.empty())
                 {
                     current = temp.top();
                     temp.pop();
                     current = current->right;
                 }
             }
             printf("
    ");
         }
         void inOrder(TreeNode *root) 
         {
             stack<TreeNode *> temp;
             TreeNode *current = root;
             while (current || !temp.empty())
             {
                 while (current)
                 {
                     temp.push(current);
                     current = current->left;
                 }
                 if (!temp.empty())
                 {
                     current = temp.top();
                     temp.pop();
                     printf("%d ", current->val);
                     current = current->right;
                 }
             }
             printf("
    ");
         }
         void postOrder(TreeNode *root) 
         {
             stack<TreeNode *> temp;
             stack<int> tempResult;;
             TreeNode *current = root;
             temp.push(root);
             while (!temp.empty())
             {
                 current = temp.top();
                 temp.pop();
                 tempResult.push(current->val);
                 if (current->left)
                 {
                     temp.push(current->left);
                 }
                 if (current->right)
                 {
                     temp.push(current->right);
                 }
             }
             while (!tempResult.empty())
             {
                 printf("%d ", tempResult.top());
                 tempResult.pop();
             }
             printf("
    ");
         }
     };
    int main()
    {
        TreeNode *root = new TreeNode(1);
        TreeNode *left = new TreeNode(2);
        TreeNode *right = new TreeNode(3);
        TreeNode *left_left = new TreeNode(4);
        TreeNode *left_right = new TreeNode(5);
        TreeNode *right_left = new TreeNode(6);
        TreeNode *right_right = new TreeNode(7);
    
        root->left = left;
        root->right = right;
        left->left = left_left;
        left->right = left_right;
        right->left = right_left;
        right->right = right_right;
    
        Solution so;
        so.preOrder(root);
        so.inOrder(root);
        so.postOrder(root);
        return 0;
    }
    既然选择了远方,便只顾风雨兼程
  • 相关阅读:
    @Aspect 注解使用详解
    Mysql的4个隔离级别
    【学习笔记】二分类问题中的“最大似然”与“交叉熵损失”概念的理解
    【转】对“先验概率”与“后验概率”概念的通俗理解
    nginx——安装部署vue项目
    JWT
    Vue——自定义组件实现vmodel
    Vue——子级向父级传递参数
    SpringBoot2(十四)全局异常切面
    Vue——ElementUI表格分页
  • 原文地址:https://www.cnblogs.com/Forever-Road/p/7026255.html
Copyright © 2011-2022 走看看