如何做国外外贸网站,在网站中添加百度地图,网络推广员怎么做,单页设计模板https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/ 题目要求给每个结点的next指针进行填充#xff0c;使每个结点的next指针指向其下一个右侧结点。如果右侧没有结点#xff0c;则将next指针设置为空。 struct Node* connect(struct Node* root) {… https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/ 题目要求给每个结点的next指针进行填充使每个结点的next指针指向其下一个右侧结点。如果右侧没有结点则将next指针设置为空。 struct Node* connect(struct Node* root) {if(root NULL){return root;}//创建一个队列同时将第一层节点加入队列中struct Node* Q[5000];int left 0, right 0;Q[right] root;//while循环的迭代是层数while(left right){//记录的当前队列的大小int size right - left;for(int i0 ; i size;i){//取出队头元素struct Node* node Q[left];//链接if(i size-1){node-next Q[left];}//拓展下一层的节点if(node-left){Q[right] node-left;}if(node-right){Q[right] node-right;}}}//返回根节点return root;
} 由于next指针已经提前设置为NULL所以我们只需要注意结点与结点之间的链接。