二叉树的遍历:前序遍历、中序遍历、后序遍历、层序遍历(C++解法代码)

二叉树的遍历通常有三种方法:迭代法,递归法,Morris算法。

前序遍历

LeetCode 144. 二叉树的前序遍历 https://leetcode-cn.com/problems/binary-tree-preorder-traversal/

方法1:迭代法

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> ans;
        if (!root) {
            return ans;
        }
        stack<TreeNode *> stk;
        stk.emplace(root);
        while (!stk.empty()) {
            TreeNode *node = stk.top();
            stk.pop();
            ans.emplace_back(node->val);
            if (node->right) {
                stk.emplace(node->right);
            }
            if (node->left) {
                stk.emplace(node->left);
            }
        }
        return ans;
    }
}; // 迭代法

方法2:递归法

class Solution {
private:
    void dfs(TreeNode *root, vector<int>& ans) {
        if (!root) {
            return;
        }
        ans.emplace_back(root->val);
        dfs(root->left, ans);
        dfs(root->right, ans);
    }

public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> ans;
        if (!root) {
            return ans;
        }
        dfs(root, ans);
        return ans;
    }
}; // 递归法

方法3:Morris算法

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> ans;
        if (!root) {
            return ans;
        }
        while (root) {
            if (!root->left) {
                ans.emplace_back(root->val);
                root = root->right;
            }
            else {
                TreeNode *node = root->left;
                while (node->right && node->right != root) {
                    node = node->right;
                }
                if (!node->right) {
                    ans.emplace_back(root->val);
                    node->right = root;
                    root = root->left;
                }
                else {
                    node->right = nullptr;
                    root = root->right;
                }
            }
        }
        return ans;
    }
}; // Morris算法

中序遍历

LeetCode 94. 二叉树的中序遍历 https://leetcode-cn.com/problems/binary-tree-inorder-traversal/

方法1:迭代法

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> ans;
        if (!root) {
            return ans;
        }
        stack<TreeNode*> stk;
        TreeNode *node = root;
        while (!stk.empty() || node) {
            while (node) {
                stk.emplace(node);
                node = node->left;
            }
            node = stk.top();
            stk.pop();
            ans.emplace_back(node->val);
            node = node->right;
        }
        return ans;
    }
}; // 迭代法

方法2:递归法

class Solution {
private:
    void dfs(TreeNode *root, vector<int>& ans) {
        if (!root) {
            return;
        }
        dfs(root->left, ans);
        ans.emplace_back(root->val);
        dfs(root->right, ans);
    }

public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> ans;
        if (!root) {
            return ans;
        }
        dfs(root, ans);
        return ans;
    }
}; // 递归法

方法3:Morris算法

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> ans;
        if (!root) {
            return ans;
        }
        while (root) {
            if (!root->left) {
                ans.emplace_back(root->val);
                root = root->right;
            }
            else {
                TreeNode *node = root->left;
                while (node->right && node->right != root) {
                    node = node->right;
                }
                if (!node->right) {
                    node->right = root;
                    root = root->left;
                }
                else {
                    ans.emplace_back(root->val);
                    node->right = nullptr;
                    root = root->right;
                }
            }
        }
        return ans;
    }
}; // Morris算法

后序遍历

LeetCode 145. 二叉树的后序遍历 https://leetcode-cn.com/problems/binary-tree-postorder-traversal/

方法1:迭代法

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> ans;
        if (!root) {
            return ans;
        }
        stack<TreeNode*> stk;
        stk.emplace(root);
        while (!stk.empty()) {
            TreeNode *node = stk.top();
            stk.pop();
            ans.emplace_back(node->val);
            if (node->left) {
                stk.emplace(node->left);
            }
            if (node->right) {
                stk.emplace(node->right);
            }
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }
}; // 迭代法

方法2:递归法

class Solution {
private:
    void dfs(TreeNode *root, vector<int>& ans) {
        if (!root) {
            return;
        }
        dfs(root->left, ans);
        dfs(root->right, ans);
        ans.emplace_back(root->val);
    }

public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> ans;
        if (!root) {
            return ans;
        }
        dfs(root, ans);
        return ans;
    }
}; // 递归法

方法3:Morris算法

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> ans;
        if (!root) {
            return ans;
        }
        while (root) {
            if (!root->right) {
                ans.emplace_back(root->val);
                root = root->left;
            }
            else {
                TreeNode *node = root->right;
                while (node->left && node->left != root) {
                    node = node->left;
                }
                if (!node->left) {
                    ans.emplace_back(root->val);
                    node->left = root;
                    root = root->right;
                }
                else {
                    node->left = nullptr;
                    root = root->left;
                }
            }
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }
}; // Morris算法

层序遍历

LeetCode 102. 二叉树的层序遍历 https://leetcode-cn.com/problems/binary-tree-level-order-traversal/

方法1:迭代法

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> ans;
        if (!root) {
            return ans;
        }
        queue<TreeNode*> q;
        q.emplace(root);
        while (!q.empty()) {
            int s = q.size();
            vector<int> t;
            for (int i = 0; i < s; ++i) {
                TreeNode *node = q.front();
                q.pop();
                t.emplace_back(node->val);
                if (node->left) {
                    q.emplace(node->left);
                }
                if (node->right) {
                    q.emplace(node->right);
                }
            }
            ans.emplace_back(t);
        }
        return ans;
    }
}; // 迭代法

方法2:递归法

class Solution {
private:
    void dfs(TreeNode *root, vector<vector<int>>& ans, int depth) {
        if (!root) {
            return;
        }
        if (ans.size() >= depth + 1) {
            ans[depth].emplace_back(root->val);
        }
        else {
            ans.emplace_back(vector<int> {root->val});
        }
        dfs(root->left, ans, depth + 1);
        dfs(root->right, ans, depth + 1);
    }
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> ans;
        dfs(root, ans, 0);
        return ans;
    }
}; // 递归法

本人只是一名编程小白,写博客是为了总结和交流,恳请大家批评指正!

你可能感兴趣的:(编程题目,c++,算法,数据结构)