代码随想录 day14

144. 二叉树的前序遍历

递归

// 前序:中 左 右
class Solution {
public:
    vector<int> ans;
    void traversal(TreeNode* cur) {
        if (nullptr == cur) {
            return;
        }
        ans.push_back(cur->val);    // 中
        traversal(cur->left);  // 左
        traversal(cur->right); // 右
    }
    vector<int> preorderTraversal(TreeNode *root) {
        ans.clear();
        if (nullptr == root) return ans;
        traversal(root);
        return ans;
    }
};

迭代

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

        while (!st.empty()) {
            TreeNode *cur = st.top();
            st.pop();

            ans.push_back(cur->val);                // 中

            // 注意:栈先入后出
            // 加入栈的节点需要考虑栈的特点
            if (cur->right) st.push(cur->right); // 右
            if (cur->left)  st.push(cur->left);  // 左
        }
        return ans;
    }
};

统一迭代

class Solution {
    // 使用空节点标记实现统一遍历
public:
    vector<int> preorderTraversal(TreeNode *root) {
        stack<TreeNode*> st;
        vector<int> ans;
        if (nullptr != root) st.push(root);
        while (!st.empty()) {
            TreeNode *cur = st.top();
            st.pop();
            
            if (nullptr != cur) {
                if (cur->right) st.push(cur->right);    // 右
                if (cur->left)  st.push(cur->left);     // 左
                
                // 中
                st.push(cur);
                st.push(nullptr);
            } else {
                cur = st.top();
                st.pop();
                ans.push_back(cur->val);
            }
        }
        return ans;
    }
};

94. 二叉树的中序遍历

递归

class Solution {
public:
    vector<int> ans;
    void traversal(TreeNode *cur) {
        if (nullptr == cur) return;
        traversal(cur->left);  // 左
        ans.push_back(cur->val);   // 中
        traversal(cur->right); // 右
    }

    vector<int> inorderTraversal(TreeNode *root) {
        ans.clear();
        if (nullptr == root) return ans;
        traversal(root);
        return ans;
    }
};

迭代

class Solution {
    // 注意:中序遍历的特点
    // 一直寻找左孩子,直到为 nullptr
public:
    vector<int> inorderTraversal(TreeNode *root) {
        vector<int> ans;
        stack<TreeNode*> st;
        TreeNode *cur = root;

        while (cur != nullptr || !st.empty()) {
            if (nullptr != cur) {
                st.push(cur);
                cur = cur->left;         // 左
            } else {
                cur = st.top(); st.pop();
                ans.push_back(cur->val); // 中
                cur = cur->right;        // 右
            }
        }
        return ans;
    }
};

统一迭代

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

        while (!st.empty()) {
            TreeNode *cur = st.top();
            st.pop();
            if (nullptr != cur) {
                if (cur->right) st.push(cur->right);       // 右
                
                st.push(cur);                              // 中
                st.push(nullptr);
                
                if (cur->left)  st.push(cur->left);        // 左
            } else {
                cur = st.top();
                st.pop();
                ans.push_back(cur->val);
            }
        }
        return ans;
    }
};

145. 二叉树后序遍历

递归

class Solution {
public:
    vector<int> ans;
    void traversal(TreeNode *cur) {
        if (nullptr == cur) return;
        traversal(cur->left);  // 左
        traversal(cur->right); // 右
        ans.push_back(cur->val);   // 中
    }

    vector<int> postorderTraversal(TreeNode *root) {
        ans.clear();
        if (nullptr == root) return ans;
        traversal(root);
        return ans;
    }
};

迭代

// 前后遍历相互转换
// 前序 中 左 右
// 后序 左 右 中
// 中 左 右 调整顺序得到 中 右 左 反转结果得到 左 右 中
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        stack<TreeNode*> st;
        vector<int> ans;
        if (nullptr != root) st.push(root);

        while (!st.empty()) {
            TreeNode* cur = st.top();
            st.pop();
            ans.push_back(cur->val);              // 中

            if (cur->left) st.push(cur->left); // 左
            if (cur->right)st.push(cur->right);// 右
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }
};

统一迭代

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

        while (!st.empty()) {
            TreeNode *cur = st.top();
            st.pop();

            if (nullptr != cur) {
                st.push(cur);
                st.push(nullptr);
                
                if (cur->right) st.push(cur->right); // 右
                if (cur->left)  st.push(cur->left);  // 左
            } else {
                cur = st.top();
                st.pop();
                ans.push_back(cur->val);
            }
        }
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值