Pre-order, mid-order and post-order traversal of binary trees (recursive and non-recursive)

 Preorder traversal

144. Preorder traversal of binary trees - LeetCode

recursion

class Solution {
private:
    vector<int> res;
public:
    void backing(TreeNode* root)
    {
        if(!root) return;
        res.push_back(root->val);
        if(root->left) backing(root->left);
        if(root->right) backing(root->right);
    }
    vector<int> preorderTraversal(TreeNode* root) {
        backing(root);
        return res;
    }
};

non-recursive

Since the order of preorder traversal is,root->left->right, when using a stack to save temporary elements, It is necessary to push the right node first, and then the left node, so that when the elements are taken out, they can be taken out in the correct order

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        stack<TreeNode*> st;//使用一个栈用于保存临时节点
        vector<int> res;//保存结果

        if(!root) return res;//若树为空,则直接返回
        
        st.push(root);
        while(!st.empty())
        {   
            //取出栈顶节点
            TreeNode* node=st.top();
            st.pop();

            //访问
            res.push_back(node->val);

            //先压入右节点,再压入左节点
            if(node->right) st.push(node->right);
            if(node->left) st.push(node->left);
        }

        return res;
    }
};

inorder traversal

94. In-order traversal of binary trees - LeetCode

recursion

class Solution {
private:
    vector<int> res;
public:
    void backing(TreeNode* root)
    {
        if(!root) return;
        if(root->left) backing(root->left);
        res.push_back(root->val);
        if(root->right) backing(root->right);
    }
    vector<int> inorderTraversal(TreeNode* root) {
        backing(root);
        return res;
    }
};

non-recursive

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        stack<TreeNode*> st;
        vector<int> res;
        if(!root) return res;
        TreeNode* cur=root;
        while(cur || !st.empty())
        {
            while(cur)
            {
                st.push(cur);
                cur=cur->left;
            }
            cur=st.top();
            st.pop();
            res.push_back(cur->val);
            cur=cur->right;
        }
        return res;
    }
};

Postorder traversal

145. Post-order traversal of binary trees - LeetCode

recursion

class Solution {
private:
    vector<int> res;
public:
    void backing(TreeNode* root)
    {
        if(!root) return;
        if(root->left) backing(root->left);
        if(root->right) backing(root->right);
        res.push_back(root->val);
    }

    vector<int> postorderTraversal(TreeNode* root) {
        backing(root);
        return res;
    }
};

non-recursive

A trick is borrowed here. We know that the order of preorder traversal is root->left->right, then if Weadjust the traversal order to root->right->left, and finally reverse the order of the results Come over, becomes the left->right-> root, that is < a i=9>The order of post-order traversal

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        stack<TreeNode*> st;
        vector<int> res;
        if(!root) return res;
        //先遍历出根->右->左的顺序结果
        st.push(root);
        while(!st.empty())
        {
            TreeNode* node=st.top();
            st.pop();
            res.push_back(node->val);
            if(node->left) st.push(node->left);
            if(node->right) st.push(node->right);
        }
        
        //将根->右->左的顺序结果倒过来之后,就变成了左->右->根的遍历顺序
        reverse(res.begin(),res.end());
        return res;
    }
};

Guess you like

Origin blog.csdn.net/qq_58158950/article/details/134872706