二叉树的递归和非递归遍历(前序中序后序层序)

Posted Moua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树的递归和非递归遍历(前序中序后序层序)相关的知识,希望对你有一定的参考价值。

二叉树的前序遍历

递归

class Solution {
public:
    void _preorderTraversal(vector<int>& v,TreeNode* root)
    {
        //递归实现
        if(root == nullptr)
            return;
        v.push_back(root->val);
        _preorderTraversal(v,root->left);
        _preorderTraversal(v,root->right);
    }
    vector<int> preorderTraversal(TreeNode* root) {
        //递归实现
        vector<int> ret;
        _preorderTraversal(ret,root);
        return ret;

    }
};

非递归

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {

        //非递归实现
        vector<int> ret;
        stack<TreeNode*> st;
        TreeNode* cur = root;
        while(cur || !st.empty())
        {
            while(cur)
            {
                st.push(cur);
                ret.push_back(cur->val);
                cur = cur->left;
            }
            //根节点出栈,右子树入栈
            TreeNode* tmp = st.top();
            st.pop();
            cur = tmp->right;
        }
        return ret;
    }
};

二叉树的中序遍历

递归

class Solution {
public:
    //递归
    void _inorderTraversal(vector<int>& v,TreeNode* root)
    {
        if(root == nullptr)
            return;
        _inorderTraversal(v,root->left);
        v.push_back(root->val);
        _inorderTraversal(v,root->right);
    }
    vector<int> inorderTraversal(TreeNode* root) {
        //递归
        vector<int> ret;
        _inorderTraversal(ret,root);
        return ret;
    }
};

非递归

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        //非递归
        stack<TreeNode*> st;
        vector<int> ret;
        TreeNode* cur = root;
        while(cur || !st.empty())
        {
            //左子树入栈
            while(cur)
            {
                st.push(cur);
                cur = cur->left;
            }
            //打印左子树和跟节点,并将右子树入栈
            while(!st.empty() && cur == nullptr)
            {
                TreeNode* tmp = st.top();
                st.pop();
                ret.push_back(tmp->val);
                cur = tmp->right;
            }
        }
        return ret;
    }
};

二叉树的后序遍历

递归

class Solution {
public:
    void _postorderTraversal(vector<int>& v,TreeNode* root)
    {
        if(root == nullptr)
            return;
        _postorderTraversal(v,root->left);
        _postorderTraversal(v,root->right);
        v.push_back(root->val);
    }
    vector<int> postorderTraversal(TreeNode* root) {
        //递归
        vector<int> ret;
        _postorderTraversal(ret,root);
        return ret;
    }
};

非递归

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        //非递归
        stack<TreeNode*> st;
        vector<int> ret;
        TreeNode* cur = root;
        TreeNode* prev = nullptr;
        while(cur || !st.empty())
        {
            //左子树入栈
            while(cur)
            {
                st.push(cur);
                cur = cur->left;
            }
            //右子树为空或者右子树已经如果栈就打印并出栈
            TreeNode* tmp = st.top();
            if(tmp->right == nullptr || tmp->right == prev)
            {
                ret.push_back(tmp->val);
                st.pop();
                
                prev = tmp;

                cur = nullptr;
            }
            else
            {
                cur = tmp->right;
            }
        }
        return ret;
    }
};

二叉树的层序遍历

二维输出

//方法1
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> ret;
        if(root == nullptr)
            return ret;
        queue<TreeNode*> parent;
        queue<TreeNode*> child;
        parent.push(root);
        while(!(parent.empty() && child.empty())) 
        {
            //保存parent队里,并将其孩子节点保存到child队列中
            vector<int> v;
            while(!parent.empty())
            {
                TreeNode* tmp = parent.front();
                v.push_back(tmp->val);
                parent.pop();
                if(tmp->left)
                    child.push(tmp->left);
                if(tmp->right)
                    child.push(tmp->right);
            }
            ret.push_back(v);
            //parent = child
            while(!child.empty())
            {
                TreeNode* tmp1 = child.front();
                parent.push(tmp1);
                child.pop();
            }
        }
        return ret;     
    }
};

//方法2
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        queue<TreeNode*> q;
        vector<vector<int>> ret;
        if(root == nullptr)
            return ret;
        q.push(root);
        while(!q.empty())
        {
            //记录当前层的个数
            int lenght = q.size();
            vector<int> tmp;
            //以个数出当前层,并将孩子节点入队
            while(lenght--)
            {
                TreeNode* head = q.front();
                tmp.push_back(head->val);
                q.pop();
                if(head->left)
                    q.push(head->left);
                if(head->right)
                    q.push(head->right);
            }
            ret.push_back(tmp);
        }
        return ret;
    }
};

从最后一层往前输出

//方法1
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        queue<TreeNode*> q;
        vector<vector<int>> ret;
        if(root == nullptr)
            return ret;
        q.push(root);
        while(!q.empty())
        {
            //记录当前层的个数
            int lenght = q.size();
            vector<int> tmp;
            //以个数出当前层,并将孩子节点入队
            while(lenght--)
            {
                TreeNode* head = q.front();
                tmp.push_back(head->val);
                q.pop();
                if(head->left)
                    q.push(head->left);
                if(head->right)
                    q.push(head->right);
            }
            ret.insert(ret.begin(),tmp);
        }
        return ret;
    }
};

 

以上是关于二叉树的递归和非递归遍历(前序中序后序层序)的主要内容,如果未能解决你的问题,请参考以下文章

Java实现二叉树的前序中序后序层序遍历(递归方法)

二叉树的前序中序后序层序遍历,递归和迭代两大类解题思路,每类细分不同解法完整版附PDF文档

二叉树的四种遍历方式以及中序后序前序中序前序后序层序创建二叉树专为力扣刷题而打造

二叉树的四种遍历方式以及中序后序前序中序前序后序层序创建二叉树专为力扣刷题而打造

二叉树的四种遍历方式以及中序后序前序中序前序后序层序创建二叉树专为力扣刷题而打造

手撕二叉树的4种遍历:前序中序后序层序