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

西部网站管理助手flash网站用什么做

西部网站管理助手,flash网站用什么做,做网站人家直接百度能搜到的,商城网站 免费开源文章目录 1.原题2.算法思想2.1.求树的高度2.2.求路径 3.关键代码4.完整代码5.输出结果 1.原题 试编写算法#xff0c;求给定二叉树上从根节点到叶子节点的一条路径长度等于树的深度减一的路径#xff08;即列出从根节点到该叶子节点的节点序列#xff09;#xff0c;若这样… 文章目录 1.原题2.算法思想2.1.求树的高度2.2.求路径 3.关键代码4.完整代码5.输出结果 1.原题 试编写算法求给定二叉树上从根节点到叶子节点的一条路径长度等于树的深度减一的路径即列出从根节点到该叶子节点的节点序列若这样的路径存在多条则输出路径终点在“最左”的一条。 2.算法思想 计算出树的高度然后再进行类前序遍历输出路径为最深的、最靠左的一条。分为两部分。1.求树的高度2.求路径 2.1.求树的高度 这个算法在14年某个题已经涉及到了。通过遍历的方式得出每一个结点的左子树高度和右子树高度而该结点的高度就是左子树高度和右子树高度较大的那一个1 2.2.求路径 由于是求最靠左的一条、最深的一条路径那么用类先序遍历的方式进行探索最有优势。根据先序遍历的特点我们第一次找到的路径即为最左边的。同时引入了一个变量isFound这样在找到一条路径之后可以更快跳过后续的递归嵌套。 3.关键代码 /*** struct treeNode* brief 二叉树节点的结构体*/ struct treeNode {int data; /** 节点存储的数据 */struct treeNode *lchild; /** 左子节点指针 */struct treeNode *rchild; /** 右子节点指针 */ };/*** brief 计算二叉树的高度。** 通过递归地计算左右子树的高度返回树的高度。** param root 二叉树根节点指针。* return 返回二叉树的高度。*/ int treeHeight(struct treeNode *root) {if (root NULL) {return 0; // 如果根节点为空返回0表示树的高度为0} else {int leftHeight treeHeight(root-lchild); // 递归计算左子树的高度int rightHeight treeHeight(root-rchild); // 递归计算右子树的高度// 返回左右子树中较大的高度加上当前节点的高度加1return (leftHeight rightHeight) ? leftHeight 1 : rightHeight 1;} }/*** brief 在二叉树中查找具有特定深度的路径** 通过递归遍历二叉树查找根到叶子节点的路径其深度等于指定高度并打印该路径。** param root 二叉树根节点指针。* param isFound 指向一个布尔值的指针用于判断是否已找到特定深度的路径。* param height 期望的路径深度。* param depth 当前递归深度。* param path 用于存储路径节点值的数组。*/ void findPath(struct treeNode *root, bool *isFound, int height, int depth, int path[]) {if (root NULL || (*isFound) true) {return; // 如果根节点为空或已找到路径则直接返回}path[depth] root-data; // 将当前节点值存入路径数组中if (depth height - 1) {(*isFound) true; // 标记已找到路径for (int i 0; i height; i) {printf(%d , path[i]); // 打印路径节点值}printf(\n);return;} else {depth 1; // 增加深度findPath(root-lchild, isFound, height, depth, path); // 递归遍历左子树findPath(root-rchild, isFound, height, depth, path); // 递归遍历右子树} }/*** brief 寻找二叉树中指定深度的路径** 通过计算二叉树的高度创建对应高度的数组并在树中查找深度为高度减一的路径。* 找到后打印该路径上的节点值。** param root 二叉树根节点指针。*/ void findSpecialPath(struct treeNode *root) {int height treeHeight(root); // 计算二叉树的高度int path[height]; // 创建与高度相同大小的数组用于存储路径节点值bool isFound false; // 指示是否找到特定深度的路径findPath(root, isFound, height, 0, path); // 查找特定深度的路径 }4.完整代码 #include stdio.h #include stdlib.h #include stdbool.h/*** struct treeNode* brief 二叉树节点的结构体*/ struct treeNode {int data; /** 节点存储的数据 */struct treeNode *lchild; /** 左子节点指针 */struct treeNode *rchild; /** 右子节点指针 */ };/*** brief 计算二叉树的高度。** 通过递归地计算左右子树的高度返回树的高度。** param root 二叉树根节点指针。* return 返回二叉树的高度。*/ int treeHeight(struct treeNode *root) {if (root NULL) {return 0; // 如果根节点为空返回0表示树的高度为0} else {int leftHeight treeHeight(root-lchild); // 递归计算左子树的高度int rightHeight treeHeight(root-rchild); // 递归计算右子树的高度// 返回左右子树中较大的高度加上当前节点的高度加1return (leftHeight rightHeight) ? leftHeight 1 : rightHeight 1;} }/*** brief 在二叉树中查找具有特定深度的路径** 通过递归遍历二叉树查找根到叶子节点的路径其深度等于指定高度并打印该路径。** param root 二叉树根节点指针。* param isFound 指向一个布尔值的指针用于判断是否已找到特定深度的路径。* param height 期望的路径深度。* param depth 当前递归深度。* param path 用于存储路径节点值的数组。*/ void findPath(struct treeNode *root, bool *isFound, int height, int depth, int path[]) {if (root NULL || (*isFound) true) {return; // 如果根节点为空或已找到路径则直接返回}path[depth] root-data; // 将当前节点值存入路径数组中if (depth height - 1) {(*isFound) true; // 标记已找到路径for (int i 0; i height; i) {printf(%d , path[i]); // 打印路径节点值}printf(\n);return;} else {depth 1; // 增加深度findPath(root-lchild, isFound, height, depth, path); // 递归遍历左子树findPath(root-rchild, isFound, height, depth, path); // 递归遍历右子树} }/*** brief 寻找二叉树中指定深度的路径** 通过计算二叉树的高度创建对应高度的数组并在树中查找深度为高度减一的路径。* 找到后打印该路径上的节点值。** param root 二叉树根节点指针。*/ void findSpecialPath(struct treeNode *root) {int height treeHeight(root); // 计算二叉树的高度int path[height]; // 创建与高度相同大小的数组用于存储路径节点值bool isFound false; // 指示是否找到特定深度的路径findPath(root, isFound, height, 0, path); // 查找特定深度的路径 }/*** brief 创建新的二叉树节点。** param data 新节点存储的数据。* return 指向新节点的指针。*/ struct treeNode *createNode(int data) {struct treeNode *newNode (struct treeNode *) malloc(sizeof(struct treeNode));newNode-data data;newNode-lchild NULL;newNode-rchild NULL;return newNode; }/*** brief 以括号表示法打印二叉树。** param root 二叉树根节点指针。*/ void printBinaryTree(struct treeNode *root) {if (root NULL) {return;}printf((%d, root-data);if (root-lchild ! NULL || root-rchild ! NULL) {printf( );if (root-lchild NULL) {printf(( ));} else {printBinaryTree(root-lchild);}printf( );if (root-rchild NULL) {printf(( ));} else {printBinaryTree(root-rchild);}}printf()); }/*** brief 以树的结构方式打印二叉树。** param root 二叉树根节点指针。* param space 节点之间的间距。*/ void printTreeStructure(struct treeNode *root, int space) {if (root NULL) {return;}int count 5; // 调整节点之间的间距printTreeStructure(root-rchild, space count);for (int i 0; i space; i) {printf( );}printf(%d\n, root-data);printTreeStructure(root-lchild, space count); }int main() {struct treeNode *root createNode(10); // 根节点为10root-lchild createNode(5);root-rchild createNode(15);root-lchild-lchild createNode(3);root-rchild-lchild createNode(12);root-rchild-rchild createNode(18);root-rchild-lchild-rchild createNode(13);root-rchild-rchild-rchild createNode(9);// 以括号表示法打印二叉树printf(Binary Tree Structure (Parenthesis Representation):\n);printBinaryTree(root);// 以树的结构方式打印二叉树printf(\nBinary Tree Structure:\n);printTreeStructure(root, 0);int height treeHeight(root);printf(Height of the tree: %d\n, height);// 寻找特定路径并打印printf(Special Path with length equal to tree depth - 1: \n);findSpecialPath(root);return 0; }5.输出结果
http://www.dnsts.com.cn/news/18350.html

相关文章:

  • 做网站需要会什么 知乎wordpress搜索框选择分类
  • 龙游县住房和城乡建设局网站wordpress 屏蔽ftp
  • 焦作专业做网站公司个人网站前置审批项
  • 天门网站网站建设简单的静态网站
  • 优化网站方法vue小程序开发教程
  • 简单详细搭建网站教程视频网站后台插件下载
  • 泉州网站建设案例网站备案容易通过吗
  • 德成建设集团有限公司网站wordpress 后台添加文章 没编辑功能
  • 大连 响应式网站公益机构网站建设方案
  • 网站建设的几点体会网站诊断方法
  • 百度收录不了网站怎样在中国建设银行网站开通短信提醒
  • 泉州专业网站制作定制石家庄中企动力
  • 本网站只做信息展示不提供在线交易网络公司门头
  • 行政机关单位网站建设轻创优选地推app
  • 怀柔建设网站百度seo排名优化如何
  • 网站keywords标签怎么写广州制作网站公司哪家好
  • 海宁网站建设公司推荐代码生成器属于什么工具
  • 贵阳网站建设zu97慈溪网站建设网站推广
  • 贵阳网站开发外包公司甘肃酒泉建设银行网站
  • wordpress多站点版个人网页制作完整教程
  • python做的网站如何部署中秋节ppt模板免费下载
  • 望江县住房和城乡建设局网站隆尧建设局网站
  • 做曖网站路飞和女帝做h的网站
  • 郑州网站建设排行宁夏网站建设电话
  • 广州 四合一网站开发百度浏览器主页网址
  • 程序员自己做网站怎么赚钱关键词投放
  • 外贸网站推广公司最大做网站代码用什么软件
  • 网站设计制作行业排行中国互联网协会副会长名单
  • 企业免费网站制作广东短视频推广效果好
  • 重庆网站建设 优化网站建设捌金手指下拉七