二叉树三种遍历方式的两种实现方式

二叉树的前中后序遍历递归实现

前序:

/**
 * Definition for a binary tree node.
 * 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 Solution {
    
    
public:
    void qianxu( TreeNode* root, vector<int>& nums){
    
    
        if( root == NULL){
    
    
            return;
        }
        nums.push_back( root->val);
        qianxu( root->left, nums);
        qianxu( root->right, nums);
        
    }
    vector<int> preorderTraversal(TreeNode* root) {
    
    
        vector<int> result;
        qianxu( root, result);
        return result;
    }
};

中序:

/**
 * Definition for a binary tree node.
 * 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 Solution {
    
    
public:
    void zhongxu( TreeNode* root ,vector<int>& nums){
    
    
        if( root == NULL){
    
    
            return;
        }
        zhongxu( root->left, nums);
        nums.push_back( root->val);
        zhongxu(root->right, nums);
    }
    vector<int> inorderTraversal(TreeNode* root) {
    
    
        vector<int> result;
        zhongxu( root, result);
        return result;
    }
};

后序:

/**
 * Definition for a binary tree node.
 * 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 Solution {
    
    
public:
    void houxu( TreeNode*root ,vector<int> & nums){
    
    
        if( root == NULL){
    
    
            return;
        }
        
        houxu( root->left, nums);
        houxu( root->right, nums);
        nums.push_back(root->val);
    }
    vector<int> postorderTraversal(TreeNode* root) {
    
    
        vector<int>result;
        houxu(root, result);
        return result;
    }
};

二叉树的前中后序遍历迭代实现

前序:

/**
 * Definition for a binary tree node.
 * 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 Solution {
    
    
public:
    vector<int> preorderTraversal(TreeNode* root) {
    
    
        vector<int>result;
        stack<TreeNode*> st;
        if( root == NULL){
    
    
            return result;
        }
        st.push(root);
        while( !st.empty()){
    
    
            TreeNode* temple = st.top();
            result.push_back(temple->val);
            st.pop();
            if( temple->right != NULL){
    
    
                st.push(temple->right);
            }
            if( temple->left != NULL){
    
    
                st.push(temple->left);
            }
        }
        return result;
    }
};

中序:
前序的代码跟中序是不通用的!首先我们要明确在刚刚的迭代中进行了两个操作:

  • 处理:将元素放入result中
  • 访问:遍历节点

而由于前序遍历顺序为中左右,而这就使得访问和处理的顺序是一样的,我们先访问到中然后处理中然后再访问左处理左,以此类推。但是中序遍历顺序是左中右,我们的访问顺序是中左右,这是冲突的!
因此在写中序遍历需要利用指针来帮助记录对节点的访问,栈则用于处理元素
在中序中,由于我们始终要先访问其左孩子,因此一开始应该是不断向左孩子深入,直到其左孩子为空再开始返回并处理。因此代码为:

/**
 * Definition for a binary tree node.
 * 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 Solution {
    
    
public:
    vector<int> inorderTraversal(TreeNode* root) {
    
    
        vector<int> result;
        if( root == NULL){
    
    
            return result;
        }
        stack<TreeNode*> st;
        TreeNode* cur = root;
        while( cur != NULL || !st.empty()){
    
    
            if( cur != NULL){
    
    
                st.push(cur);
                cur = cur->left;
            }
            else{
    
    
                cur = st.top();
                st.pop();
                result.push_back(cur->val);
                cur = cur->right;
            }
        }
        return result;
    }
};

后序:
后序的顺序是左右中,而前序的顺序是中左右,那么如果把前序的代码调整一下让左孩子先入栈,即可变成中右左,此时再将结果数组翻转,即可得到左右中的遍历结果
具体代码如下:

/**
 * Definition for a binary tree node.
 * 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 Solution {
    
    
public:
    vector<int> postorderTraversal(TreeNode* root) {
    
    
        vector<int>result;
        if( root == NULL){
    
    
            return result;
        }
        stack<TreeNode*> st;
        st.push(root);
        while( !st.empty()){
    
    
            TreeNode* tem = st.top();
            st.pop();
            result.push_back( tem->val);
            if( tem->left != NULL){
    
    
                st.push(tem->left);
            }
            if( tem->right != NULL){
    
    
                st.push(tem->right);
            }
        }
        reverse( result.begin(), result.end());
        return result;
    }
};

二叉树的统一遍历法

在上面中我们实现了三种遍历方式可是其风格并不统一,这不利于我们对迭代法的掌握。
那么上述不统一的主要原因是在中序遍历中我们访问节点和处理节点的顺序不一样,才导致我们无法统一其写法。
那么如果要统一的话,我们具体的做法就是:

那我们就将访问的节点放入栈中,把要处理的节点也放入栈中但是要做标记
如何标记呢?就是要处理的节点放入栈之后,紧接着放入一个空指针作为标记。 这种方法也可以叫做标记法。

那么我们在遍历的时候,当访问到空节点时,即可认为在栈中该空节点的下一个节点就是应该处理的(取其val放入result中),那么如果没有访问到空节点,就认为该节点还不应该被处理,就将其左右孩子按照顺序放入,以及它自己需要再放入,那么它自己再放入这次就需要加上空节点作为标记。
因此各代码如下:
前序:

/**
 * Definition for a binary tree node.
 * 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 Solution {
    
    
public:
    vector<int> preorderTraversal(TreeNode* root) {
    
    
        vector<int>result;
        if (root == NULL){
    
    
            return result;
        }
        stack<TreeNode*> st;
        st.push(root);
        while( !st.empty()){
    
    
            TreeNode* cur = st.top();
            if (cur != NULL){
    
    
                // 如果不为空那么不需要进行处理
                st.pop();
                if( cur->right != NULL){
    
    
                    st.push(cur->right);
                }
                if( cur->left != NULL){
    
    
                    st.push(cur->left);
                }
                st.push(cur);
                st.push(NULL);
            }
            else{
    
    
                st.pop();
                cur = st.top();
                result.push_back(cur->val);
                st.pop();
            }
        }
        return result;
    }
};

中序:

/**
 * Definition for a binary tree node.
 * 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 Solution {
    
    
public:
    vector<int> inorderTraversal(TreeNode* root) {
    
    
        vector<int>result;
        if (root == NULL){
    
    
            return result;
        }
        stack<TreeNode*> st;
        st.push(root);
        while( !st.empty()){
    
    
            TreeNode* cur = st.top();
            if (cur != NULL){
    
    
                // 如果不为空那么不需要进行处理
                st.pop();
                if( cur->right != NULL){
    
    
                    st.push(cur->right);
                }
                st.push(cur);
                st.push(NULL);
                if( cur->left != NULL){
    
    
                    st.push(cur->left);
                }

            }
            else{
    
    
                st.pop();
                cur = st.top();
                result.push_back(cur->val);
                st.pop();
            }
        }
        return result;
    }
};

后序:

/**
 * Definition for a binary tree node.
 * 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 Solution {
    
    
public:
    vector<int> postorderTraversal(TreeNode* root) {
    
    
        vector<int>result;
        if (root == NULL){
    
    
            return result;
        }
        stack<TreeNode*> st;
        st.push(root);
        while( !st.empty()){
    
    
            TreeNode* cur = st.top();
            if (cur != NULL){
    
    
                // 如果不为空那么不需要进行处理
                st.pop();
                st.push(cur);
                st.push(NULL);
                if( cur->right != NULL){
    
    
                    st.push(cur->right);
                }

                if( cur->left != NULL){
    
    
                    st.push(cur->left);
                }

            }
            else{
    
    
                st.pop();
                cur = st.top();
                result.push_back(cur->val);
                st.pop();
            }
        }
        return result;
    }
};

猜你喜欢

转载自blog.csdn.net/StarandTiAmo/article/details/134393884