• 代码题(8)— 二叉树的遍历:前、中、后序


    1、144. 二叉树的前序遍历

    (1)递归

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<int> res;
        vector<int> preorderTraversal(TreeNode* root) {
            if(root == nullptr)
                return res;
            res.push_back(root->val);
            preorderTraversal(root->left);
            preorderTraversal(root->right);
            return res;
        }
    };

    (2)非递归

    /**
     * Definition for a binary tree node.
     * 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 == nullptr)
                return res;
            stack<TreeNode*> stNode;
            while(!stNode.empty() || root)
            {
                while(root)
                {
                    stNode.push(root);
                    res.push_back(root->val);
                    root = root->left;
                }
                root = stNode.top();
                stNode.pop();
                root = root->right;
            }
            return res;
        }
    };

    2、94. 二叉树的中序遍历

    (1)递归

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<int> res;
        vector<int> inorderTraversal(TreeNode* root) {
            if(root != nullptr)
            {
                inorderTraversal(root->left);
                res.push_back(root->val);
                inorderTraversal(root->right);
            }
            return res;
        }
    };

    (2)非递归

    /**
     * Definition for a binary tree node.
     * 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 == nullptr)
                return res;
            stack<TreeNode*> stNode;
            while(!stNode.empty() || root)
            {
                while(root)
                {
                    stNode.push(root);
                    root = root->left;
                }
                root = stNode.top();
                stNode.pop();
                res.push_back(root->val);
                root = root->right;
            }
            return res;
        }
    };

    3、145. 二叉树的后序遍历

    (1)递归

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<int> res;
        vector<int> postorderTraversal(TreeNode* root) {
            if(root == nullptr)
                return res;
            postorderTraversal(root->left);
            postorderTraversal(root->right);  
            res.push_back(root->val);
            return res;
        }
    };

    (2)非递归

    /**
     * Definition for a binary tree node.
     * 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 == nullptr)
                return res;
            stack<TreeNode*> stNode;
            TreeNode* pre = nullptr;
            while(!stNode.empty() || root)
            {
                while(root)
                {
                    stNode.push(root);
                    root = root->left;
                }
                TreeNode* cur = stNode.top();
                if(cur->right == nullptr || pre == cur->right)  //如果没有右节点,或者右节点被访问过,则可以访问该节点
                {
                    res.push_back(cur->val);
                    stNode.pop();
                    pre = cur;
                }
                else
                    root = cur->right;
            }
            return res;
        }
    };

      该方法与上面区别是:不需要添加新的节点。

    /**
     * Definition for a binary tree node.
     * 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 == nullptr)
                return res;
            stack<TreeNode*> st;
            TreeNode* pre = nullptr;
            while(!st.empty() || root)
            {
                while(root)
                {
                    st.push(root);
                    root = root->left;
                }
                root = st.top(); //此处没有添加新的节点
                if(root->right == nullptr || root->right == pre)
                {
                    res.push_back(root->val);
                    st.pop();
                    pre = root;
                    root = nullptr;//此处添加将root置为空,不再访问判断该节点。
                }
                else
                    root = root->right;
            }
            return res;
        }
    };
  • 相关阅读:
    黑芝麻智能技术
    景嘉微GPU与显卡
    电子表格文档控件DevExpress Office File API v21.1
    WPF界面控件Telerik UI for WPF初级入门教程
    WPF应用程序的交互界面还有这些样式,赶紧Get
    手把手教你创建一个Windows风格的应用程序界面(Part 1)
    Web应用的数据管理教程
    Visual Studio插件CodeRush正式发布v21.1.5,免费高速下载
    Mysqldump 备份说明及数据库备份脚本分享-运维笔记
    某国有银行的超融合技术选型和应用实践
  • 原文地址:https://www.cnblogs.com/eilearn/p/9217393.html
Copyright © 2020-2023  润新知