程序员代码面试指南 3.1:实现二叉树先序、中序和后序遍历

https://www.nowcoder.com/practice/566f7f9d68c24691aa5abd8abefa798c

模板题,注意简单的建树方法。

#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left, *right;
    TreeNode(int _val) : val(_val), left(nullptr), right(nullptr) {}
};

//建立二叉树
void createTree(TreeNode* root, int& n)
{
    if (n == 0) return;

    int rootVal, leftVal, rightVal;
    cin >> rootVal >> leftVal >> rightVal;

    if (leftVal != 0)
    {
        root->left = new TreeNode(leftVal);
        createTree(root->left, -- n);
    }

    if (rightVal != 0)
    {
        root->right = new TreeNode(rightVal);
        createTree(root->right, -- n);
    }
}

//前序递归遍历
void preOrderRecursion(TreeNode* root, vector<int>& res)
{
    if (root == nullptr) return;

    res.push_back(root->val);
    preOrderIteration(root->left, res);
    preOrderIteration(root->right, res);
}

//前序迭代遍历
void preOrderIteration(TreeNode* root, vector<int>& res)
{
    stack<TreeNode*> stk;
    stk.push(root);

    while (!stk.empty())
    {
        TreeNode* node = stk.top();
        stk.pop();

        res.push_back(node->val);
        if (node->right) stk.push(node->right); //栈是先进后出,所以右节点先进
        if (node->left) stk.push(node->left);
    }
}

//中序递归遍历
void inOrderRecursion(TreeNode* root, vector<int>& res)
{
    if (root == nullptr) return;

    inOrderIteration(root->left, res);
    res.push_back(root->val);
    inOrderIteration(root->right, res);
}

//中序迭代遍历
void inOrderIteration(TreeNode* root, vector<int>& res)
{
    stack<TreeNode*> stk;
    TreeNode* p = root;

    while (p != nullptr || !stk.empty())
    {
        if (p != nullptr)       //先一直往左子树走
        {
            stk.push(p);
            p = p->left;
        }
        else                    //左子树没有了,回退同时往右子树走
        {
            p = stk.top();
            stk.pop();

            res.push_back(p->val);
            p = p->right;
        }
    }
}

//后序递归遍历
void postOrderRecursion(TreeNode* root, vector<int>& res)
{
    if (root == nullptr) return;

    postOrderIteration(root->left, res);
    postOrderIteration(root->right, res);
    res.push_back(root->val);
}

//后序迭代遍历
void postOrderIteration(TreeNode* root, vector<int>& res)
{
    stack<TreeNode*> stk;
    stk.push(root);

    while (!stk.empty())
    {
        TreeNode* node = stk.top();
        stk.pop();

        res.push_back(node->val);
        if (node->left) stk.push(node->left);
        if (node->right) stk.push(node->right);
    }

    reverse(res.begin(), res.end());    //最后要翻转
}

int main()
{
    int n, rootVal;
    cin >> n >> rootVal;

    TreeNode* root = new TreeNode(rootVal);
    createTree(root, n);

    vector<int> res;

    //preOrderIteration(root, res);
    preOrderRecursion(root, res);
    for (int &node : res) cout << node << " ";
    cout << endl;
    res.clear();

    //inOrderIteration(root, res);
    inOrderRecursion(root, res);
    for (int &node : res) cout << node << " ";
    cout << endl;
    res.clear();

    //postOrderIteration(root, res);
    postOrderRecursion(root, res);
    for (int &node : res) cout << node << " ";
    cout << endl;
    res.clear();

    return 0;
}

主要是为左程云的《程序员代码面试指南》这本书改写C++版的题解。

全部评论

相关推荐

行云流水1971:这份实习简历的优化建议: 结构清晰化:拆分 “校园经历”“实习经历” 板块(当前内容混杂),按 “实习→校园→技能” 逻辑排版,求职意向明确为具体岗位(如 “市场 / 运营实习生”)。 经历具象化:现有描述偏流程,需补充 “动作 + 数据”,比如校园活动 “负责宣传” 可加 “运营公众号发布 5 篇推文,阅读量超 2000+,带动 300 + 人参与”;实习内容补充 “协助完成 XX 任务,效率提升 X%”。 岗位匹配度:锚定目标岗位能力,比如申请运营岗,突出 “内容编辑、活动执行” 相关动作;申请市场岗,强化 “资源对接、数据统计” 细节。 信息精简:删减冗余表述(如重复的 “负责”),用短句分点,比如 “策划校园招聘会:联系 10 + 企业,组织 200 + 学生参与,到场率达 85%”。 技能落地:将 “Office、PS” 绑定经历,比如 “用 Excel 整理活动数据,输出 3 份分析表;用 PS 设计 2 张活动海报”,避免技能单独罗列。 优化后需强化 “经历 - 能力 - 岗位需求” 的关联,让实习 / 校园经历的价值更直观。 若需要进一步优化服务,私信
实习,投递多份简历没人回...
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务