当前位置: 首页 > news >正文

网站建设网站建设哪里有网站设计制作一条龙多少钱

网站建设网站建设哪里有,网站设计制作一条龙多少钱,网站如何排版,关键词app下载1.题目要求: 给定一个二叉树:struct Node {int val;Node *left;Node *right;Node *next; } 填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL 。初始状态下,所有 ne…

1.题目要求:

给定一个二叉树:struct Node {int val;Node *left;Node *right;Node *next;
}
填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL 。初始状态下,所有 next 指针都被设置为 NULL

在这里插入图片描述
2.做题步骤:
(1)先创建好队列结构体,入队函数,出队函数:

//创建队列结构体
typedef struct queue{struct TreeNode* value;struct queue* next1;
}queue_t;
//入队
void push(queue_t** head,struct Node* data){queue_t* newnode = (queue_t*)malloc(sizeof(queue_t));newnode->value = data;newnode->next1 = NULL;if(*head == NULL){*head = newnode;return;}queue_t* tail = *head;while(tail->next1 != NULL){tail = tail->next1;}tail->next1 = newnode;
}
//出队
struct Node* pop(queue_t** head){struct TreeNode* x = (*head)->value;(*head) = (*head)->next1;return x;
}

(2)设置变量,进行层序遍历:

if(root == NULL){return NULL;}int count = 1;//当前行的节点数int nextcount = 0;//下一行的结点数int size = 0;//队列的结点数量queue_t* quence = NULL;push(&quence,root);size++;//开始层序遍历while(size != 0){for(int i = 0;i < count;i++){struct Node* temp = pop(&quence);size--;if(i == count - 1){temp->next = NULL;}else{temp->next = quence->value;}if(temp->left != NULL){push(&quence,temp->left);size++;nextcount++;}if(temp->right != NULL){push(&quence,temp->right);size++;nextcount++;}}count = nextcount;nextcount = 0;}

全部代码:

/*** Definition for a Node.* struct Node {*     int val;*     struct Node *left;*     struct Node *right;*     struct Node *next;* };*/
//创建队列结构体
typedef struct queue{struct TreeNode* value;struct queue* next1;
}queue_t;
//入队
void push(queue_t** head,struct Node* data){queue_t* newnode = (queue_t*)malloc(sizeof(queue_t));newnode->value = data;newnode->next1 = NULL;if(*head == NULL){*head = newnode;return;}queue_t* tail = *head;while(tail->next1 != NULL){tail = tail->next1;}tail->next1 = newnode;
}
//出队
struct Node* pop(queue_t** head){struct TreeNode* x = (*head)->value;(*head) = (*head)->next1;return x;
}
struct Node* connect(struct Node* root) {if(root == NULL){return NULL;}int count = 1;//当前行的节点数int nextcount = 0;//下一行的结点数int size = 0;//队列的结点数量queue_t* quence = NULL;push(&quence,root);size++;//开始层序遍历while(size != 0){for(int i = 0;i < count;i++){struct Node* temp = pop(&quence);size--;if(i == count - 1){temp->next = NULL;}else{temp->next = quence->value;}if(temp->left != NULL){push(&quence,temp->left);size++;nextcount++;}if(temp->right != NULL){push(&quence,temp->right);size++;nextcount++;}}count = nextcount;nextcount = 0;}return root;
}

好了,这就是我的全部代码了,大家如果觉得好的话,给个免费的赞吧,谢谢了^ _ ^

http://www.yayakq.cn/news/669476/

相关文章:

  • 网站关键词做排名不分安全证查询官网
  • 出站链接对网站有什么影响做产品类网站
  • 网站托管维护合同北京蓝杉网站建设公司
  • 珠海网站开发哪家好17网一起做网店潮汕池尾
  • 做网站 写脚本是什么wordpress的文章插件
  • 试百客 专业做试用的网站学做网站多少钱
  • 北京网站建设好吗衡阳房产网站建设
  • 济南专业做网站的公司百度权重高的网站
  • 青蛙网站建设全国二级建造师注册查询系统入口
  • 东莞网站营销推广公司wordpress如何自定义导航栏
  • 网站建设怎么进行一级域名申请网络传销是否传销
  • 网站建设合集视频 wordpress 主题
  • wordpress免登陆接口杭州seo价格
  • 外管局网站上做预收登记腾讯云域名交易平台
  • 南京网站设计哪家好网页传奇游戏源码
  • 旅行网站的建设目录青岛李沧网站建设
  • 开网店 建网站要钱吗网站怎么做精准引流
  • 制作深圳网站建设综合门户网站什么意思
  • 农家乐网站 建设广州优化网站建设
  • 龙岗住房和建设局网站官网无锡市住房和城乡建设部网站
  • 鲜花网站建设结构布局湖南手机网站建设
  • 旅游网站开发方案公司网站开发和设计 怎么开票
  • 为什么检测行业不能用网站做手机商城网站案例
  • 搭建外文网站vs2010做网站登陆界面
  • 网站是先制作后上线么台州营销型网站建设
  • 重庆市住房和城乡建设厅官方网站查询好吊顶网站
  • 创业邦使用什么网站模板新网站如何做免费推广
  • 网页介绍怎么写免费seo技术教程视频
  • 福建省网站备案用户注销装饰公司东莞网站建设
  • 企业移动网站品牌做网站需要哪些技术知乎