代码随想录算法训练营第十四天| 递归遍历|迭代遍历|

递归遍历

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    void traversal(TreeNode* root,vector<int>&res){
        if(root==nullptr)
        return ;
        // traversal(root->left,res);
        res.push_back(root->val);
         traversal(root->left,res);
        traversal(root->right,res);
    }
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int>res;
        traversal(root,res);
        return res;
    }
};

迭代遍历

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int>res;
        stack<TreeNode*>sta;
        if(root!=nullptr) sta.push(root);
            while(!sta.empty()){
            TreeNode *node=sta.top();
            sta.pop();
            res.push_back(node->val);
            if(node->right!=nullptr)sta.push(node->right);
            if(node->left!=nullptr)sta.push(node->left);
        }
        return res;
    }
};

统一风格迭代遍历

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        stack<TreeNode*>sta;
         vector<int>res;
        if(root!=nullptr) sta.push(root);
        while(!sta.empty()){
            TreeNode*node=sta.top();
            if(node!=nullptr){
                sta.pop();
                if(node->right!=nullptr)sta.push(node->right);
                sta.push(node);
                sta.push(nullptr);
                if(node->left!=nullptr)sta.push(node->left);
            }else{
                sta.pop();
                node=sta.top();
                res.push_back(node->val);
                sta.pop();
            }
        }
        return res;
    }
};
相关推荐
weixin_527550401 小时前
初级程序员入门指南
javascript·python·算法
思捻如枫2 小时前
C++数据结构和算法代码模板总结——算法部分
数据结构·c++
嘉陵妹妹3 小时前
深度优先算法学习
学习·算法·深度优先
GalaxyPokemon3 小时前
LeetCode - 53. 最大子数组和
算法·leetcode·职场和发展
小猫咪怎么会有坏心思呢3 小时前
华为OD机考 - 水仙花数 Ⅰ(2025B卷 100分)
数据结构·链表·华为od
hn小菜鸡4 小时前
LeetCode 1356.根据数字二进制下1的数目排序
数据结构·算法·leetcode
zhuiQiuMX4 小时前
分享今天做的力扣SQL题
sql·算法·leetcode
music&movie5 小时前
算法工程师认知水平要求总结
人工智能·算法
laocui16 小时前
Σ∆ 数字滤波
人工智能·算法
yzx9910136 小时前
Linux 系统中的算法技巧与性能优化
linux·算法·性能优化