校招笔试,关于链表和二叉树以及图的读取处理,ACM模式

校招笔试链表acm模式,写好之后把head传给对应的方法即可,就可以自己实现acm的链表翻转,链表排序等等啦。

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>


using namespace std;

struct LinkNode
{
	int _val;
	LinkNode* next;
	LinkNode(int val) : _val (val), next(NULL){};
	LinkNode(int val, LinkNode* next1) : _val(val), next(next1) {};
};


int main()
{
	LinkNode* head = new LinkNode(-1);
	LinkNode* pre = head;
	LinkNode* cur = NULL;
	int n;
	while (cin >> n)
	{
		if (n == -1) break;
		cur = new LinkNode(n);
		pre->next = cur;
		pre = cur;
		if (getchar() == '\n') break;

	}

	cur = head->next;

	while (cur)
	{
		cout << cur->_val << "->";
		cur = cur->next;
	}
	cout << endl;
	return 0;

}

那么这个就是二叉树啦,会写下面这个可以自己实现二叉树的翻转,求深度等等问题啦,只需把root传给方法即可。

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

//定义树节点
struct TreeNode
{
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode():val(0),left(nullptr),right(nullptr){}
    TreeNode(int _val):val(_val),left(nullptr),right(nullptr){}
    TreeNode(int _val,TreeNode* _left,TreeNode* _right):val(0),left(_left),right(_right){}
};

//根据数组生成树
TreeNode* buildTree(const vector<int>& v)
{
    vector<TreeNode*> vTree(v.size(),nullptr);
    TreeNode* root = nullptr;
    for(int i = 0; i < v.size(); i++)
    {
        TreeNode* node = nullptr;
        if(v[i] != -1)
        {
            node = new TreeNode(v[i]);
        }
        vTree[i] = node;
    }
    root = vTree[0];
    for(int i = 0; 2 * i + 2 < v.size(); i++)
    {
        if(vTree[i] != nullptr)
        {
            vTree[i]->left = vTree[2 * i + 1];
            vTree[i]->right = vTree[2 * i + 2];
        }
    }
    return root;
}

//根据二叉树根节点层序遍历并打印
void printBinaryTree(TreeNode* root)
{
    if(root == nullptr) return;
    vector<vector<int>> ans;
    queue<TreeNode*> q;
    q.push(root);
    while(!q.empty())
    {
        int size = q.size();
        vector<int> path;
        for(int i = 0;i<size;i++)
        {
            TreeNode* node = q.front();
            q.pop();
            if(node == nullptr)
            {
                path.push_back(-1);
            }
            else
            {
                path.push_back(node->val);
                q.push(node->left);
                q.push(node->right);
            }
        }
        ans.push_back(path);
    }
    
    for(int i = 0;i<ans.size();i++)
    {
        for(int j = 0;j<ans[i].size();j++)
        {
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }
    return;
}

int main()
{
	// 验证
    vector<int> v = {4,1,6,0,2,5,7,-1,-1,-1,3,-1,-1,-1,8};
    TreeNode* root = buildTree(v);
    printBinaryTree(root);
    
    return 0;
}

但其实,校招里的树和图的读取更多是邻接表的方式,如下
5
1 2
1 3
1 6
2 3
6 3

vector<int> e[1001];

// ...

// 图的读入
int n;
cin >> n;
// 由于是树,所以只需要读n - 1 条边
for (int i = 1 ; i < n ; i++){
    int x , y;
    cin >> x >> y;
    e[x].push_back(y);
    // 由于你无法确认x,y之间谁是父亲节点,所以需要存双向边,
    // 在dfs的过程中防止返祖即可(返祖会引发死递归!)。
    e[y].push_back(x);
    // PS:在有一些题目里,他会明确规定谁是父亲,这种情况下就不用存双向边,
    // 且在dfs的过程中也不用担心返祖的问题.
}

这样可以把图的关系储存起来啦!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值