二叉树的遍历(迭代法、递归法、Morris 遍历)

144. 二叉树的前序遍历

方法1:递归法

class Solution {
private:
    void traversal(TreeNode* cur, vector<int>& vec) {
        if(cur == NULL) return;
        vec.emplace_back(cur->val);
        if(cur->left) traversal(cur->left, vec);
        if(cur->right) traversal(cur->right, vec);
        return;
    }
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int>res;
        if(root == NULL) return res;
        traversal(root, res);
        return res;
    }
};

$时间复杂度O(n),空间复杂度O(n)

方法2:迭代法

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        stack<TreeNode*> st;
        vector<int> res;
        if (root != NULL) st.push(root);

        while(!st.empty()) {
            TreeNode* nod = st.top();
            if (nod != NULL) {
                st.pop();
                if (nod->right) st.push(nod->right);
                if (nod->left) st.push(nod->left);
                st.push(nod);
                st.push(NULL);
            } else {
                st.pop();
                nod = st.top();
                st.pop();
                res.emplace_back(nod->val);
            }
        }
        return res;
    }
};

$时间复杂度O(n),空间复杂度O(n)

方法3:Morris 遍历

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        TreeNode* cur = nullptr;

        while (root != nullptr) {
            if (root->left != nullptr) {
                cur = root->left;
                while (cur->right != nullptr && cur->right != root) cur = cur->right;
                if (cur->right == nullptr) {
                    res.emplace_back(root->val);
                    cur->right = root;
                    root = root->left;
                    continue;
                } else {
                    cur->right = nullptr;
                }
            } else {
                res.emplace_back(root->val);
            }
            root = root->right;
        }
        return res;
    }
};

$时间复杂度O(n),空间复杂度O(1)

94. 二叉树的中序遍历

方法1:递归法

class Solution {
private:
    void traversal(TreeNode* cur, vector<int>& vec) {
        if(cur == NULL) return;
        if(cur->left) traversal(cur->left, vec);
        vec.emplace_back(cur->val);
        if(cur->right) traversal(cur->right, vec);
        return;
    }
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        if (root == NULL) return res;
        traversal(root, res);
        return res;
    }
};

$时间复杂度O(n),空间复杂度O(n)

方法2:迭代法

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        stack<TreeNode*> st;
        vector<int> res;
        if (root != NULL) st.push(root);

        while(!st.empty()) {
            TreeNode* nod = st.top();
            if (nod != NULL) {
                st.pop();
                if (nod->right) st.push(nod->right);
                st.push(nod);
                st.push(NULL);
                if (nod->left) st.push(nod->left);
            } else {
                st.pop();
                nod = st.top();
                st.pop();
                res.emplace_back(nod->val);
            }
        }
        return res;
    }
};

$时间复杂度O(n),空间复杂度O(n)

方法3:Morris 中序遍历

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        TreeNode* cur = nullptr;
        if (root == nullptr) return res;

        while (root != nullptr) {
            if (root->left != nullptr) {
                cur = root->left;
                while (cur->right != nullptr && cur->right != root) {
                    cur = cur->right;
                }

                if (cur->right == nullptr) {
                    cur->right = root;
                    root = root->left;
                } else {
                    res.emplace_back(root->val);
                    root = root->right;
                    cur->right = nullptr;
                }
            } else {
                res.emplace_back(root->val);
                root = root->right;
            }
        }
        return res;
    }
};

$时间复杂度O(n),空间复杂度O(1)

145. 二叉树的后序遍历

方法1:递归法

class Solution {
private:
    void traversal(TreeNode* cur, vector<int>& vec) {
        if (cur == NULL) return;
        if (cur->left) traversal(cur->left, vec);
        if (cur->right) traversal(cur->right, vec);
        vec.emplace_back(cur->val);
        return;
    }
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> res;
        if (root == NULL) return res;
        traversal(root, res);
        return res;
    }
};

$时间复杂度O(n),空间复杂度O(n)

方法2:迭代法

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        stack<TreeNode*> st;
        vector<int> res;
        if (root != NULL) st.push(root);

        while(!st.empty()) {
            TreeNode* nod = st.top();
            if (nod != NULL) {
                st.pop();
                st.push(nod);
                st.push(NULL);
                if (nod->right) st.push(nod->right);
                if (nod->left) st.push(nod->left);
            } else {
                st.pop();
                nod = st.top();
                st.pop();
                res.emplace_back(nod->val);
            }
        }
        return res;
    }
};

$时间复杂度O(n),空间复杂度O(n)

方法3:Morris

class Solution {
private:
    void reverseTree(vector<int>& vec, TreeNode* nod) {
        int cnt = 0;
        while (nod != nullptr) {
            vec.emplace_back(nod->val);
            nod = nod->right;
            ++cnt;
        }
        reverse(vec.end() - cnt, vec.end());
    }
    
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> res;
        TreeNode* cur = nullptr, * ver = root;
        if (ver == nullptr) return res;
        while (ver != nullptr) {
            if (ver->left != nullptr) {
                cur = ver->left;
                while (cur->right != nullptr && cur->right != ver) cur = cur->right;
                if (cur->right == nullptr) {
                    cur->right = ver;
                    ver = ver->left;
                    continue;
                } else {
                    cur->right = nullptr;
                    reverseTree(res, ver->left);
                }
            }
            ver = ver->right;
        }
        reverseTree(res, root);
        return res;
    }
};

$时间复杂度O(n),空间复杂度O(1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值