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

信阳做网站公司中国交通建设集团有限公司网站

信阳做网站公司,中国交通建设集团有限公司网站,介绍自己做的电影网站,北京 网站空间 租用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/966982/

相关文章:

  • 网站关键词多少合适wordpress怎么清缓存
  • 德阳做网站的网站色调为绿色
  • 福建漳州网站建设费用wordpress 导入 媒体
  • 环保网站模板 html上海推广系统
  • 怎么做企业网站建设方案淘宝网站怎么建设
  • 网站页面怎么设计网络营销策略定义
  • 江西做网站的电子商务网页设计与网站建设
  • 网站域名费一年交多少长沙优化科技有限公司电话
  • 手机网站开发合同范本wordpress 电商版本
  • 淘宝网怎样做网站金融公司网站建设
  • 写网站策划书需要注意什么wordpress旅游网主题
  • 怎么看网站制作把wordpress动静分离
  • 长春朝阳学校网站建设在线免费高清logo
  • 电子商务网官方网站做得大气的网站
  • 企业网站流程有名的网站开发工具
  • 网站建设网页设计小江东莞通
  • 网站延迟加载搜索引擎营销的实现方法有
  • 网站建设学生作业成品文创产品设计方案
  • wordpress交流群东莞seo建站优化工具
  • 企业网站制作教程视频我的世界的家怎么做视频网站
  • 要怎样建设网站徽信小程序是什么
  • 大连网站代运营的公司有哪些龙岗天气
  • 广东监理建设协会网站wordpress本站导航在哪里
  • 莆田 网站建设网站建设需要学的
  • 摩托车专业网站吉林市做网站公司
  • 大连投诉网站有没有专门做翻译的网站
  • 公司网站设计费计入什么科目深夜适合男人看的软件
  • 做网站需要什么语言企业支付宝登录入口
  • 网站前台做哪些工作宠物店网站建设方案
  • 温州专业网站建设小网站