• 二叉树遍历(前中后)


    二叉树前序遍历:

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<int> preorderTraversal(TreeNode *root) {
            vector<int> res;
            if(root==NULL)return res;
            stack<TreeNode *> sta;
            sta.push(root);
            while(!sta.empty())
            {
                TreeNode *tmp=sta.top();
                sta.pop();
                res.push_back(tmp->val);
                if(tmp->right)sta.push(tmp->right);
                if(tmp->left)sta.push(tmp->left);
            }
            return res;
        }
    /**
    void preorder(vector<int> &res, TreeNode * root)
    {
        if(root==NULL)return;
        res.push_back(root->val);
        preorder(res,root->left);
        preorder(res,root->right);
    }
        vector<int> preorderTraversal(TreeNode *root) {
            vector<int> res;
            preorder(res,root);
            return res;
        }
    */
    };
    

      

    二叉树中序遍历:

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
    vector<int> inorderTraversal(TreeNode *root) 
    {
        vector<int> res;
        if(root==NULL)return res;
        TreeNode *p=root;
        stack<TreeNode *> sta;
        while(p!=NULL||!sta.empty())
        {
            while(p!=NULL)
            {
                sta.push(p);
                p=p->left;
            }
            if(!sta.empty())
            {
                p=sta.top();
                sta.pop();
                res.push_back(p->val);
                p=p->right;
            }
        }
        return res;
    }
    /*
    void inorder(TreeNode *root, vector<int> &res)
    {
        if(root==NULL)return;
        inorder(root->left,res);
        res.push_back(root->val);
        inorder(root->right,res);
        return;
    }
        vector<int> inorderTraversal(TreeNode *root) {
            vector<int> res;
            inorder(root,res);
            return res;
        }
    */
    };
    

      

    二叉树后序遍历:

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<int> postorderTraversal(TreeNode *root) {
            vector<int> res;
            if(root==NULL)return res;
            map<TreeNode *, int> smap;
            smap[root]=0;
            stack<TreeNode *> sta;
            sta.push(root);
            while(!sta.empty())
            {
                TreeNode *p=sta.top();
                if((!p->right&&!p->left)||smap.count(p->right)||smap.count(p->left))
                {
                    res.push_back(p->val);
                    smap[p]=1;
                    sta.pop();
                }
                else
                {
                   if(p->right&&!smap.count(p->right))
                   {
                    sta.push(p->right);
                    smap[p->right]=0;
                   }
                   if(p->left&&!smap.count(p->left))
                   {
                    sta.push(p->left);
                    smap[p->left]=0;
                   }
                }
            }
            return res;
        }
    /**
    void postorder(vector<int> & res,TreeNode *root)
    {
        if(root==NULL)return;
        postorder(res,root->left);
        postorder(res,root->right);
        res.push_back(root->val);
    }
        vector<int> postorderTraversal(TreeNode *root) {
            vector<int> res;
            postorder(res,root);
            return res;
        }
    */
    };
    

      

  • 相关阅读:
    C++入门经典-例4.9-输出不同生命周期的变量值
    C++入门经典-例4.8-同名的全局变量和局部变量
    C++入门经典-例4.7-变量的作用域
    C++入门经典-例4.6-使用重载函数
    C++入门经典-例4.5-利用循环求n的阶乘
    C++入门经典-例4.4-循环嵌套之求n的阶乘
    C++入门经典-例4.3-函数的递归调用之汉诺塔问题
    C++入门经典-例4.2-调用默认参数的函数
    C++入门经典-例4.1-声明、定义和使用函数
    C++入门经典-例3.25-使用循环输出闰年
  • 原文地址:https://www.cnblogs.com/Vae1990Silence/p/4830552.html
Copyright © 2020-2023  润新知