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

递归遍历

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;
    }
};
相关推荐
科大饭桶39 分钟前
数据结构自学Day5--链表知识总结
数据结构·算法·leetcode·链表·c
小高Baby@39 分钟前
map数据结构在Golang中是无序的,并且键值对的查找效率较高的原因
数据结构
北风toto40 分钟前
python学习DataFrame数据结构
数据结构·python·学习
我爱C编程3 小时前
基于Qlearning强化学习的1DoF机械臂运动控制系统matlab仿真
算法
chao_7893 小时前
CSS表达式——下篇【selenium】
css·python·selenium·算法
chao_7893 小时前
Selenium 自动化实战技巧【selenium】
自动化测试·selenium·算法·自动化
YuTaoShao3 小时前
【LeetCode 热题 100】24. 两两交换链表中的节点——(解法一)迭代+哨兵
java·算法·leetcode·链表
怀旧,3 小时前
【数据结构】8. 二叉树
c语言·数据结构·算法
泛舟起晶浪3 小时前
相对成功与相对失败--dp
算法·动态规划·图论
地平线开发者4 小时前
地平线走进武汉理工,共建智能驾驶繁荣生态
算法·自动驾驶