日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2006年清华大学计算机研究生机试真题

發布時間:2024/7/19 编程问答 79 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2006年清华大学计算机研究生机试真题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://ac.jobdu.com/problem.php?pid=1078 二叉樹遍歷

#include<stdio.h> #include<string.h>//二叉樹結點的描述 typedef struct BinaryTreeNode {char data;struct BinaryTreeNode *lchild, *rchild; //左右孩子 }BinaryTreeNode,*BinaryTree;inline BinaryTreeNode* ConstructCore(char *startPreorder, char *endPreorder, char *startInorder, char *endInorder) {//前序遍歷序列的第一個字符是根節點的值char rootValue = startPreorder[0];BinaryTreeNode* root = new BinaryTreeNode();root->data = rootValue;root->lchild = root->rchild = NULL;if(startPreorder == endPreorder){if(startInorder == endInorder && *startPreorder== *startInorder)return root;}//在中序遍歷中找到根節點的值char *rootInorder = startInorder;while(rootInorder <= endInorder && *rootInorder != rootValue)rootInorder++;int leftLength = rootInorder - startInorder;char *leftPreorderEnd = startPreorder + leftLength;if(leftLength > 0){//構建左子樹root->lchild = ConstructCore(startPreorder+1, leftPreorderEnd, startInorder, rootInorder-1);}if(leftLength < endPreorder - startPreorder){//構建右子樹root->rchild = ConstructCore(leftPreorderEnd+1, endPreorder, rootInorder+1, endInorder);}return root; }//分別在前序遍歷和中序遍歷的序列中確定左、右子樹的子序列 inline BinaryTreeNode* Construct(char *preorder, char *inorder, int length) {if(preorder == NULL || inorder == NULL || length<=0)return NULL;return ConstructCore(preorder, preorder+length-1, inorder, inorder+length-1); }//后序遍歷 inline void PostOrder(BinaryTreeNode *root) {if(root==NULL)return ;PostOrder(root->lchild); //遞歸調用,前序遍歷左子樹PostOrder(root->rchild); //遞歸調用,前序遍歷右子樹printf("%c", root->data); //輸出數據 }//釋放二叉樹 inline void DeleteBinaryTree(BinaryTree root) {if(root){DeleteBinaryTree(root->lchild); //釋放左子樹DeleteBinaryTree(root->rchild); //釋放右子樹delete root; //釋放根結點} }int main(void) {int length;char preorder[27],inorder[27];while(scanf("%s",preorder)!=EOF){scanf("%s",inorder);length = strlen(preorder);BinaryTreeNode* root = Construct(preorder, inorder, length);PostOrder(root);printf("\n");DeleteBinaryTree(root);}return 0; }


?

總結

以上是生活随笔為你收集整理的2006年清华大学计算机研究生机试真题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。