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

144. 二叉树的前序遍历

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

//递归法
class Solution {
public:
    void preorder(TreeNode* cur, vector& ans){
        if(cur == nullptr) return;
        ans.push_back(cur->val);
        preorder(cur->left,ans);
        preorder(cur->right,ans);
    }
    vector preorderTraversal(TreeNode* root) {
        vector ans;
        preorder(root,ans);
        return ans;
    }
};
//非递归法(栈模拟)
class Solution {
public:
    vector preorderTraversal(TreeNode* root) {
        vector ans;
        stack 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& ans){
        if(cur == nullptr) return;
        inorder(cur->left,ans);
        ans.push_back(cur->val); 
        inorder(cur->right,ans);   
    }
    vector inorderTraversal(TreeNode* root) {
        vector ans;
        inorder(root,ans);
        return ans;
    }
};
//非递归法(栈模拟)
class Solution {
public:
    vector inorderTraversal(TreeNode* root) {
        vector ans;
        stack 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& ans){
        if(cur == nullptr) return;
        postorder(cur->left,ans);
        postorder(cur->right,ans);
        ans.push_back(cur->val); 
    }
    vector postorderTraversal(TreeNode* root) {
        vector ans;
        postorder(root,ans);
        return ans;
    }
};
//非递归法(栈模拟)
class Solution {
public:
    vector postorderTraversal(TreeNode* root) {
        vector ans;
        stack 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 inorderTraversal(TreeNode* root) {
        vector result;
        stack 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;
    }
};

        

你可能感兴趣的:(代码随想录算法训练营,算法,leetcode,二叉树)