C++算法基础之树篇(上)

在刷的题的过程中,总结树的一些模板参考。

//****迭代版本*******
//模板
vector<int> res;
stack<TreeNode*> s;
TreeNode* cur = root;
while (cur != nullptr || !s.empty()) {
    if (cur != nullptr) {
    	//内容
    }
    else {
        cur = s.top();
        s.pop();
    }
}

根据模板,我们可以轻松完成对前序和中序的遍历,至于后序是先由左右根->根右左,套模板后再使用反转即可。

//前序
vector<int> preorderTraversal(TreeNode* root) {
    vector<int> res;
    stack<TreeNode*> s;
    TreeNode* cur = root;
    while (cur != nullptr || !s.empty()) {
        if (cur != nullptr) {
            s.push(cur);
            res.push_back(cur->val); //根
            cur = cur->left; //左
        }
        else {
            cur = s.top();
            s.pop();
            cur = cur->right; //右
        }
    }
    return res;
}

//中序
vector<int> inorderTraversal(TreeNode* root) {
    vector<int> res;
    stack<TreeNode*> s;
    TreeNode* cur = root;
    while (cur != nullptr || !s.empty()) {
        if (cur != nullptr) {
            s.push(cur);
            cur = cur->left; //左
        }
        else {
            cur = s.top();
            res.push_back(cur->val); //根
            s.pop();
            cur = cur->right; //右
        }
    }
    return res;
}

//后序
vector<int> postorderTraversal(TreeNode* root) {
    // 左右根 -> 根右左
    vector<int> res;
    stack<TreeNode*> s;
    stack<int> resStack;
    TreeNode* cur = root;
    while (!s.empty() || cur != nullptr) {
        if (cur != nullptr) {
            resStack.push(cur->val); //根
            s.push(cur);
            cur = cur->right; //右
        }
        else {
            cur = s.top();
            s.pop();
            cur = cur->left;//左
        }
    }
    while (!resStack.empty()) {
        res.push_back(resStack.top());
        resStack.pop();
    }
    return res;

}

接下来的层序遍历和N叉树的遍历类似,不同的是while循环里面用的是!s.empty()

//层序 //剑指offer 32
vector<vector<int>> levelOrder(TreeNode* root) {
    vector <vector <int>> ret;
    if (!root) {
        return ret;
    }
    queue <TreeNode*> q;
    q.push(root);
    while (!q.empty()) {
        int currentLevelSize = q.size();
        ret.push_back(vector <int>());
        for (int i = 1; i <= currentLevelSize; ++i) {
            auto node = q.front(); q.pop();
            ret.back().push_back(node->val);
            if (node->left) q.push(node->left);
            if (node->right) q.push(node->right);
        }
    }
    return ret;
}

//N叉树前序遍历
vector<int> preorder(Node* root) {
    vector<int> res;
    stack<Node*> s;
    s.push(root);
    if (root == nullptr) return res;
    //Node* cur = root;
    while (!s.empty()) {
        Node* cur = s.top();
        res.push_back(cur->val); //根
        s.pop();
        for (int i = cur->children.size() - 1; i >= 0; --i) {
            s.push(cur->children[i]); //右左 -> 左右
        }
    }
    return res;
}

//N叉树后序遍历

vector<int> postorder(Node* root) {
    vector<int> res;
    stack<Node*> s;
    s.push(root);
    if (root == nullptr) return res;
    //Node* cur = root;
    while (!s.empty()) {
        Node* cur = s.top();
        res.push_back(cur->val); //根
        s.pop();
        for (int i = 0; i < cur->children.size(); ++i) {
            s.push(cur->children[i]); //左右->右左
        }
    }
    reverse(res.begin(), res.end());
    return res;
}

//N叉树的层序遍历
vector<vector<int>> levelOrder(Node* root) {
    vector<vector<int>> res;
    if (root == nullptr) return res;
    queue<Node*> q;
    q.push(root);
    while (!q.empty()) {
        vector<int> Subres;
        //Node* temp = q.front();
        int size = q.size();
            for(int i = 0; i < size; ++i) {
                Node* cur = q.front();
                q.pop();
                Subres.push_back(cur->val);
                for (int j = 0; j < cur->children.size(); ++j) {
                    if (cur->children[j]) q.push(cur->children[j]);
                }
            }
        res.push_back(Subres);
    }
    return res;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值