代码随想录算法训练营第十四天| LeetCode144. 二叉树的前序遍历 94. 二叉树的中序遍历145.二叉树的后序遍历

144. 二叉树的前序遍历

题目:144. 二叉树的前序遍历

//递归法
class Solution {
public:
    void preorder(TreeNode* cur, vector<int>& ans){
        if(cur == nullptr) return;
        ans.push_back(cur->val);
        preorder(cur->left,ans);
        preorder(cur->right,ans);
    }
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> ans;
        preorder(root,ans);
        return ans;
    }
};
//非递归法(栈模拟)
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> ans;
        stack<TreeNode*> sta;
        sta.push(root);
        while(!sta.empty()){
            TreeNode* cur = sta.top();
            sta.pop();
            if(cur != nullptr) ans.push_back(cur->val);
            else continue;
            sta.push(cur->right);
            sta.push(cur->left); 
        } 
        return ans;
    }
};

94. 二叉树的中序遍历

题目:94. 二叉树的中序遍历

//递归法
class Solution {
public:
    void inorder(TreeNode* cur, vector<int>& ans){
        if(cur == nullptr) return;
        inorder(cur->left,ans);
        ans.push_back(cur->val); 
        inorder(cur->right,ans);   
    }
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> ans;
        inorder(root,ans);
        return ans;
    }
};
//非递归法(栈模拟)
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> ans;
        stack<TreeNode*> sta;
        TreeNode* cur = root;
        while(cur != nullptr || !sta.empty()){
            if(cur != nullptr){
                sta.push(cur);
                cur = cur->left;
            }else{
                cur = sta.top();
                sta.pop();
                ans.push_back(cur->val);
                cur = cur->right;
            }
        }    
        return ans;
    }
};

145.二叉树的后序遍历

题目:145. 二叉树的后序遍历

//递归法
class Solution {
public:
    void postorder(TreeNode* cur, vector<int>& ans){
        if(cur == nullptr) return;
        postorder(cur->left,ans);
        postorder(cur->right,ans);
        ans.push_back(cur->val); 
    }
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> ans;
        postorder(root,ans);
        return ans;
    }
};
//非递归法(栈模拟)
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> ans;
        stack<TreeNode*> sta;
        sta.push(root);
        while(!sta.empty()){
            TreeNode* cur = sta.top();
            sta.pop();
            if(cur != nullptr) ans.push_back(cur->val);
            else continue;
            sta.push(cur->left); 
            sta.push(cur->right);
        } 
        reverse(ans.begin(),ans.end());
        return ans;
    }
};

总结

题型:二叉树的遍历

技巧:递归遍历,非递归遍历(用栈去模拟),尤其注意中序遍历,需要一个指针辅助访问。

        统一的非递归遍历需要借助添加空指针标记是否被访问过(了解即可)。

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> result;
        stack<TreeNode*> st;
        if (root != NULL) st.push(root);
        while (!st.empty()) {
            TreeNode* node = st.top();
            if (node != NULL) {
                st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
                if (node->right) st.push(node->right);  // 添加右节点(空节点不入栈)

                st.push(node);                          // 添加中节点
                st.push(NULL); // 中节点访问过,但是还没有处理,加入空节点做为标记。

                if (node->left) st.push(node->left);    // 添加左节点(空节点不入栈)
            } else { // 只有遇到空节点的时候,才将下一个节点放进结果集
                st.pop();           // 将空节点弹出
                node = st.top();    // 重新取出栈中元素
                st.pop();
                result.push_back(node->val); // 加入到结果集
            }
        }
        return result;
    }
};

        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值