二叉树的非递归遍历(先序, 中序, 后序)

先序遍历:

/**

 * 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) {

        stack<TreeNode *> st;

        vector<int> result;

        if(root != NULL)

            st.push(root);

        else

            return result;

        while(!st.empty()){

            TreeNode *node = st.top();

            st.pop();

            result.push_back(node->val);

            if(node->right != NULL)

                st.push(node->right);

            if(node->left != NULL)

                st.push(node->left);

        }

        return result;

    }

};

 中序遍历:

/**

 * 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> result;

        stack<TreeNode *> st;

        if(root == NULL)

            return result;

        if(root->right != NULL)

            st.push(root->right);

        st.push(root);

        TreeNode* node = root->left;

        while(node != NULL){

            if(node->right != NULL)

                st.push(node->right);

            st.push(node);

            node = node->left;

        }

        while(!st.empty()){

            node = st.top();

            st.pop();

            result.push_back(node->val);

            if(!st.empty() && st.top() == node->right){

                node = st.top();

                st.pop();

                while(node != NULL){

                    if(node->right != NULL)

                        st.push(node->right);

                    st.push(node);

                    node = node->left;

                }

            }

        }

        return result;

    }

};

 后序遍历:

/**

 * 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> result;

        stack<TreeNode *> st;

        TreeNode *node = root;

        if(node == NULL)

            return result;

        st.push(node);

        if(node->right != NULL)

            st.push(node->right);

        if(node->left != NULL)

            st.push(node->left);

        TreeNode *lastNode = NULL;

        while(!st.empty()){

            node = st.top();

            st.pop();

            if(node->left == NULL && node->right == NULL){

                result.push_back(node->val);

                lastNode = node;

            }

            else if(lastNode != NULL && (lastNode == node->left || lastNode == node->right)){

                result.push_back(node->val);

                lastNode = node;

            }

            else {

                st.push(node);

                if(node->right != NULL)

                st.push(node->right);

                if(node->left != NULL)

                    st.push(node->left);

            }

        }

        return result;

    }

};

 

你可能感兴趣的:(二叉树)