题解 | #二叉树遍历#C语言
二叉树遍历
https://www.nowcoder.com/practice/6e732a9632bc4d12b442469aed7fe9ce
#include <stdio.h> #include <string.h> char nlr[27]; char lnr[27]; void lrn(int nlr_start,int nlr_end,int lnr_start,int lnr_end) { if(nlr_start>nlr_end)return; char root=nlr[nlr_start]; int index=-1; for(int i=lnr_start;i<=lnr_end;i++) { if(root==lnr[i]) { index=i; break; } } lrn(nlr_start+1,index+nlr_start-lnr_start,lnr_start,index-1); lrn(index+nlr_start-lnr_start+1,nlr_end,index+1,lnr_end); printf("%c",root); } int main() { while (scanf("%s %s", &nlr[0], &lnr[0]) != EOF) { // 注意 while 处理多个 case lrn(0,strlen(nlr)-1,0,strlen(lnr)-1); printf("\n"); } return 0; }