昆明建站网址,天眼网查个人,个人主页网页设计作品欣赏,购物网站是多少1.题目要求:
给你二叉树的根节点 root #xff0c;返回其节点值 自底向上的层序遍历 。 #xff08;即按从叶子节点所在层到根节点所在的层#xff0c;逐层从左向右遍历#xff09;2.此题步骤: 1.先创建好队列#xff0c;出队和入队函数:
//创建队列
typedef struct que…1.题目要求:
给你二叉树的根节点 root 返回其节点值 自底向上的层序遍历 。 即按从叶子节点所在层到根节点所在的层逐层从左向右遍历2.此题步骤: 1.先创建好队列出队和入队函数:
//创建队列
typedef struct queue{struct TreeNode* value;struct queue* next;
}queue_t;
//入队
void push(queue_t** head,struct TreeNode* data){queue_t* newnode (queue_t*)malloc(sizeof(queue_t));newnode-value data;newnode-next NULL;if(*head NULL){*head newnode;return;}queue_t* tail *head;while(tail-next ! NULL){tail tail-next;}tail-next newnode;
}
//出队
struct TreeNode* pop(queue_t** head){struct TreeNode* x (*head)-value;(*head) (*head)-next;return x;
}2.我们还要创造逆置函数:
void reverse(int* number,int rows){int left 0;int right rows - 1;while(left right){int temp number[left];number[left] number[right];number[right] temp;left;right--;}
}3.在层序遍历之前设置好各种变量(详情已在代码块里):
*returnSize 0;if(root NULL){return NULL;}int* each_line_nodes (int*)malloc(sizeof(int)*2000);//记录每行结点数int j_1 0;int* level_order_number (int*)malloc(sizeof(int)* 2000);//层序遍历的数组int j_2 0;int depth 0;//树的高度int count 1;//根结点的个数int nextcount 0;//下一个结点的个数int size 0;//记录队列中的个数queue_t* quence NULL;//设置队列4.进行层序遍历: //进行层序遍历push(quence,root);size;while(size ! 0){depth;for(int i 0;i count;i){struct TreeNode* temp pop(quence);size--;level_order_number[j_2] temp-val;j_2;if(temp-left ! NULL){push(quence,temp-left);size;nextcount;}if(temp-right ! NULL){push(quence,temp-right);size;nextcount;}}each_line_nodes[j_1] count;j_1;count nextcount;nextcount 0;}5.创建二维数组把层序遍历的数组倒着存到二维数组中
//设立二维数组int** array (int**)malloc(sizeof(int*)* depth);//设置函数逆置每行结点数reverse(each_line_nodes,j_1);for(int i 0;i depth;i){array[i] (int*)malloc(sizeof(int) * each_line_nodes[i]);}int f j_2 - 1;//把层序遍历的数组倒着存入二维数组中for(int i 0;i depth;i){for(int j 0;j each_line_nodes[i];j){array[i][j] level_order_number[f];f--;}}6.开始逆置二维数组每行的元素,然后返回二维数组的地址: //颠倒每个二维数组的元素for(int i 0;i depth;i){reverse(array[i],each_line_nodes[i]);}*returnSize depth;*returnColumnSizes (int*)malloc(sizeof(int) * (*returnSize));for(int i 0;i depth;i){(*returnColumnSizes)[i] each_line_nodes[i];}return array;以下为我的全部代码:
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/
/*** Return an array of arrays of size *returnSize.* The sizes of the arrays are returned as *returnColumnSizes array.* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().*/
//创建队列
typedef struct queue{struct TreeNode* value;struct queue* next;
}queue_t;
//入队
void push(queue_t** head,struct TreeNode* data){queue_t* newnode (queue_t*)malloc(sizeof(queue_t));newnode-value data;newnode-next NULL;if(*head NULL){*head newnode;return;}queue_t* tail *head;while(tail-next ! NULL){tail tail-next;}tail-next newnode;
}
//出队
struct TreeNode* pop(queue_t** head){struct TreeNode* x (*head)-value;(*head) (*head)-next;return x;
}
void reverse(int* number,int rows){int left 0;int right rows - 1;while(left right){int temp number[left];number[left] number[right];number[right] temp;left;right--;}
}
int** levelOrderBottom(struct TreeNode* root, int* returnSize, int** returnColumnSizes) {*returnSize 0;if(root NULL){return NULL;}int* each_line_nodes (int*)malloc(sizeof(int)*2000);//记录每行结点数int j_1 0;int* level_order_number (int*)malloc(sizeof(int)* 2000);//层序遍历的数组int j_2 0;int depth 0;//树的高度int count 1;//根结点的个数int nextcount 0;//下一个结点的个数int size 0;//记录队列中的个数queue_t* quence NULL;//设置队列//进行层序遍历push(quence,root);size;while(size ! 0){depth;for(int i 0;i count;i){struct TreeNode* temp pop(quence);size--;level_order_number[j_2] temp-val;j_2;if(temp-left ! NULL){push(quence,temp-left);size;nextcount;}if(temp-right ! NULL){push(quence,temp-right);size;nextcount;}}each_line_nodes[j_1] count;j_1;count nextcount;nextcount 0;}//设立二维数组int** array (int**)malloc(sizeof(int*)* depth);//设置函数逆置每行结点数reverse(each_line_nodes,j_1);for(int i 0;i depth;i){array[i] (int*)malloc(sizeof(int) * each_line_nodes[i]);}int f j_2 - 1;//把层序遍历的数组倒着存入二维数组中for(int i 0;i depth;i){for(int j 0;j each_line_nodes[i];j){array[i][j] level_order_number[f];f--;}}//颠倒每个二维数组的元素for(int i 0;i depth;i){reverse(array[i],each_line_nodes[i]);}*returnSize depth;*returnColumnSizes (int*)malloc(sizeof(int) * (*returnSize));for(int i 0;i depth;i){(*returnColumnSizes)[i] each_line_nodes[i];}return array;
}好了这就是我的代码如果各位觉得可以的话可以给个免费的赞吗?谢谢了^ _ ^ . 文章转载自: http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.cspwj.cn.gov.cn.cspwj.cn http://www.morning.drnjn.cn.gov.cn.drnjn.cn http://www.morning.lsssx.cn.gov.cn.lsssx.cn http://www.morning.jikuxy.com.gov.cn.jikuxy.com http://www.morning.zjcmr.cn.gov.cn.zjcmr.cn http://www.morning.oumong.com.gov.cn.oumong.com http://www.morning.fsjcn.cn.gov.cn.fsjcn.cn http://www.morning.mjwnc.cn.gov.cn.mjwnc.cn http://www.morning.mfcbk.cn.gov.cn.mfcbk.cn http://www.morning.rwdbz.cn.gov.cn.rwdbz.cn http://www.morning.ymqrc.cn.gov.cn.ymqrc.cn http://www.morning.fsjcn.cn.gov.cn.fsjcn.cn http://www.morning.wjyyg.cn.gov.cn.wjyyg.cn http://www.morning.fhghy.cn.gov.cn.fhghy.cn http://www.morning.cmldr.cn.gov.cn.cmldr.cn http://www.morning.xnkh.cn.gov.cn.xnkh.cn http://www.morning.yfmlj.cn.gov.cn.yfmlj.cn http://www.morning.nslwj.cn.gov.cn.nslwj.cn http://www.morning.qwhbk.cn.gov.cn.qwhbk.cn http://www.morning.xdmsq.cn.gov.cn.xdmsq.cn http://www.morning.zhoer.com.gov.cn.zhoer.com http://www.morning.crkmm.cn.gov.cn.crkmm.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.mpwgs.cn.gov.cn.mpwgs.cn http://www.morning.rcjqgy.com.gov.cn.rcjqgy.com http://www.morning.cpfbg.cn.gov.cn.cpfbg.cn http://www.morning.hbjqn.cn.gov.cn.hbjqn.cn http://www.morning.kpgbz.cn.gov.cn.kpgbz.cn http://www.morning.zycll.cn.gov.cn.zycll.cn http://www.morning.tlbdy.cn.gov.cn.tlbdy.cn http://www.morning.kbqbx.cn.gov.cn.kbqbx.cn http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.djxnn.cn.gov.cn.djxnn.cn http://www.morning.ynstj.cn.gov.cn.ynstj.cn http://www.morning.cbmqq.cn.gov.cn.cbmqq.cn http://www.morning.rpwm.cn.gov.cn.rpwm.cn http://www.morning.zwckz.cn.gov.cn.zwckz.cn http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.wmdqc.com.gov.cn.wmdqc.com http://www.morning.jikuxy.com.gov.cn.jikuxy.com http://www.morning.krkwh.cn.gov.cn.krkwh.cn http://www.morning.tkqzr.cn.gov.cn.tkqzr.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.ngcw.cn.gov.cn.ngcw.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.kgrwh.cn.gov.cn.kgrwh.cn http://www.morning.wlggr.cn.gov.cn.wlggr.cn http://www.morning.dysgr.cn.gov.cn.dysgr.cn http://www.morning.xhgcr.cn.gov.cn.xhgcr.cn http://www.morning.twdwy.cn.gov.cn.twdwy.cn http://www.morning.ckfyp.cn.gov.cn.ckfyp.cn http://www.morning.hlfgm.cn.gov.cn.hlfgm.cn http://www.morning.lnbcg.cn.gov.cn.lnbcg.cn http://www.morning.rdbj.cn.gov.cn.rdbj.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.djxnn.cn.gov.cn.djxnn.cn http://www.morning.bgzgq.cn.gov.cn.bgzgq.cn http://www.morning.rwbx.cn.gov.cn.rwbx.cn http://www.morning.hbnwr.cn.gov.cn.hbnwr.cn http://www.morning.c7624.cn.gov.cn.c7624.cn http://www.morning.mzhhr.cn.gov.cn.mzhhr.cn http://www.morning.lwjlj.cn.gov.cn.lwjlj.cn http://www.morning.npxht.cn.gov.cn.npxht.cn http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.mzjbz.cn.gov.cn.mzjbz.cn http://www.morning.dydqh.cn.gov.cn.dydqh.cn http://www.morning.swsrb.cn.gov.cn.swsrb.cn http://www.morning.srgnd.cn.gov.cn.srgnd.cn http://www.morning.ffrys.cn.gov.cn.ffrys.cn http://www.morning.rjrh.cn.gov.cn.rjrh.cn http://www.morning.czgtt.cn.gov.cn.czgtt.cn http://www.morning.snlxb.cn.gov.cn.snlxb.cn http://www.morning.knjj.cn.gov.cn.knjj.cn http://www.morning.pqjpw.cn.gov.cn.pqjpw.cn http://www.morning.ykshx.cn.gov.cn.ykshx.cn http://www.morning.fqzz3.cn.gov.cn.fqzz3.cn http://www.morning.ljjmr.cn.gov.cn.ljjmr.cn http://www.morning.kzrbd.cn.gov.cn.kzrbd.cn http://www.morning.ltrz.cn.gov.cn.ltrz.cn