当前位置:网站首页>先序遍历,中序遍历,后序遍历,层序遍历

先序遍历,中序遍历,后序遍历,层序遍历

2022-08-09 06:34:00 未央吖

先序遍历

递归:

class Solution {
public:
    void preorder(TreeNode *root, vector<int> &res) {
        if (root == nullptr) {
            return;
        }
        res.push_back(root->val);
        preorder(root->left, res);
        preorder(root->right, res);
    }

    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> res;
        preorder(root, res);
        return res;
    }
};

迭代:栈

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        stack<TreeNode*> st;
        vector<int> ret;
        if (root) st.push(root);
        while (!st.empty()) {
            TreeNode*node = st.top();
            st.pop();
            ret.push_back(node->val);              //中
            if (node->right) st.push(node->right); //右
            if (node->left) st.push(node->left);   //左
        }return ret;
    }
};

中序遍历

递归:

class Solution {
public:
    void inorder(TreeNode* root, vector<int>& res) {
        if (!root) {
            return;
        }
        inorder(root->left, res);               //左
        res.push_back(root->val);               //中
        inorder(root->right, res);              //右
    }
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        inorder(root, res);
        return res;
    }
};

迭代:栈

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        stack<TreeNode*>st;
        TreeNode*cur = root;
        vector<int> ret;
        while (cur || !st.empty()) {
            while (cur) {                     //先遍历完所有左子树
                st.push(cur);
                cur = cur->left;
            }
            TreeNode*res = st.top();           
            st.pop();
            ret.push_back(res->val);           //中
            cur = res->right;                   //右
        }
        return ret;
    }
};

后序遍历

递归:

class Solution {
public:
    void postorder(TreeNode *root, vector<int> &res) {
        if (root == nullptr) {
            return;
        }
        postorder(root->left, res);              //左
        postorder(root->right, res);            //右
        res.push_back(root->val);               //中
    }

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

迭代:栈

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        stack<TreeNode*>st;
        vector<int> ret;
        if (root) st.push(root);
        while (!st.empty()) {
            TreeNode*cur = st.top();
            ret.push_back(cur->val);                      //中
            st.pop();
            if (cur->left) {st.push(cur->left);}          //左
            if (cur->right) {st.push(cur->right);}        //右
        }
        reverse(ret.begin(), ret.end());                  //反转
        return ret;
    }
};

层序遍历

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> ret;
        if (!root) return ret;

        queue<TreeNode*> que;
        que.push(root);
        while (!que.empty()) {
            int size = que.size();
            vector<int> res;
            for (int i = 0; i < size; i++) {
                TreeNode*cur = que.front();
                que.pop();
                res.push_back(cur->val);
                if (cur->left) {que.push(cur->left);}
                if (cur->right) {que.push(cur->right);}
            }
            ret.push_back(res);
        }
        return ret;
    }
};

原网站

版权声明
本文为[未央吖]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_58183566/article/details/126215896