
树
文章平均质量分 63
树
繁星蓝雨
Happy coding!
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
二叉树k层的叶子结点个数
层序遍历二叉树,记录某层的二叉树叶子结点个数。没有目标层时,让该层结点的孩子结点入队;到达目标层时,不再让该层的孩子结点入队,统计队中(该层)叶子结点的个数。假设二叉树采用二叉链表存储结构,设计一个算法求其指定的第k层(k>1,跟是第1层)的叶子结点个数。设置一个全局变量记录某层叶子结点个数,前序遍历二叉树,并在遍历的过程中记录结点的层数。原创 2023-12-03 22:15:00 · 723 阅读 · 0 评论 -
2017年408专业算法题
因为要转换为中序表达式,因此使用中序遍历。在中序遍历的过程中,对于当前访问的非空结点p,则先输出"(“,然后递归调用左子树,输出p的权值,递归调用右子树,输出“)”,如果p是根或者叶结点,则不需要输出“(”或”)"。原创 2023-01-31 20:39:02 · 1643 阅读 · 0 评论 -
2014年408专业算法题
WPL:带权路径长度计算原创 2023-01-31 13:34:36 · 1651 阅读 · 0 评论 -
2022年专业408算法题
2022年专业408的算法题。原创 2022-12-31 15:57:12 · 4109 阅读 · 5 评论 -
1022. 从根到叶的二进制数之和(计算所有根节点到叶结点的路径)————附带详细讲解和实例
文章目录1 题目2 思路3 代码1 题目2 思路题目的大意就是求树根节点到叶子结点的路径,然后根据路径上的权值求出每条路径的值,最后累加得到所有路径的值。求路径,使用先序遍历,每次记录遍历到的每层节点,当该节点的左右孩子均为空时,该节点为叶子节点,计算路径值。3 代码1,depth方法中,每次value值,都逐层累加计算路径值。2,depth2方法2中,每次记录路径值,当遇到叶节点时,计算路径值。class Solution {public: int res = 0;原创 2022-03-11 16:50:51 · 1613 阅读 · 0 评论 -
二叉搜索树的最近公共祖先————附带详细代码和解析
文章目录1 题目2 解析2.1 法12.2 法22.2 法23 代码3.13.2 代码1 题目2 解析2.1 法1节点ancestor从根节点向下遍历,如果ancestor->val同时小于q->val和p-val,那么公共祖先就在ancestor的右边;如果ancestor->val同时大于q->val和p-val,那么公共祖先就在ancestor的左边;如果ancestor->val不同时小于或大于q->val和p-val,那么它就是分岔节点(即两个原创 2022-01-12 21:45:00 · 315 阅读 · 0 评论 -
问题 E: 合并果子-NOIP2004TGT2
1 题目问题 E: 合并果子-NOIP2004TGT2[命题人 : 外部导入]时间限制 : 1.000 sec 内存限制 : 128 MB题目描述合并果子(fruit.pas/c/cpp)在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,...原创 2020-02-12 21:51:35 · 486 阅读 · 0 评论 -
问题 C: 哈夫曼树
1 题目问题 C: 哈夫曼树[命题人 : 外部导入]时间限制 : 1.000 sec 内存限制 : 32 MB题目描述哈夫曼树,第一行输入一个数n,表示叶结点的个数。需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和。输入输入有多组数据。每组第一行输入一个数n,接着输入n个叶节点(叶节点权值不超过100,2&...原创 2020-02-12 22:43:48 · 427 阅读 · 0 评论 -
问题 A: 算法6-12:自底向上的赫夫曼编码
1 题目在通讯领域,经常需要将需要传送的文字转换成由二进制字符组成的字符串。在实际应用中,由于总是希望被传送的内容总长尽可能的短,如果对每个字符设计长度不等的编码,且让内容中出现次数较多的字符采用尽可能短的编码,则整个内容的总长便可以减少。另外,需要保证任何一个字符的编码都不是另一个字符的编码前缀,这种编码成为前缀编码。而赫夫曼编码就是一种二进制前缀编码,其从叶子到根(自底向上)逆向求出每个...原创 2020-02-13 11:33:00 · 1633 阅读 · 0 评论 -
问题 B: 算法6-13:自顶向下的赫夫曼编码
1 题目问题 B: 算法6-13:自顶向下的赫夫曼编码[命题人 : 外部导入]时间限制 : 1.000 sec 内存限制 : 32 MB题目描述在本题中,我们将要讨论的是自顶向下的赫夫曼编码算法。从根出发,遍历整棵赫夫曼树从而求得各个叶子结点所表示的字符串。算法的关键部分可以表示如下:在本题中,读入n个字符所对应的权值,生成赫夫曼编码,并依次输出计算出的每一个赫夫曼编码。输入输...原创 2020-02-13 11:34:43 · 806 阅读 · 0 评论 -
问题 D: Haffman编码
1 题目问题 D: Haffman编码[命题人 : 外部导入]时间限制 : 1.000 sec 内存限制 : 128 MB题目描述哈弗曼编码大家一定很熟悉吧(不熟悉也没关系,自己查去。。。)。现在给你一串字符以及它们所对应的权值,让你构造哈弗曼树,从而确定每个字符的哈弗曼编码。当然,这里有一些小规定:1.规定哈弗曼树的左子树编码为0,右子树编码为1;2.若两个字符权值相同,则ASC...原创 2020-02-13 12:52:30 · 633 阅读 · 0 评论 -
1020 Tree Traversals (25分)
1 题目1020 Tree Traversals (25分)Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level ord...原创 2020-01-31 11:03:44 · 336 阅读 · 0 评论 -
1099 Build A Binary Search Tree (30分)
1 题目1099 Build A Binary Search Tree (30分)A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with ke...原创 2020-02-11 10:26:30 · 332 阅读 · 0 评论 -
1064 Complete Binary Search Tree (30分)
1 题目1064 Complete Binary Search Tree (30分)A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with k...原创 2020-02-11 09:42:36 · 543 阅读 · 0 评论 -
1043 Is It a Binary Search Tree (25分)
1 题目1043 Is It a Binary Search Tree (25分)A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with ke...原创 2020-02-10 22:44:32 · 329 阅读 · 2 评论 -
1086 Tree Traversals Again (25分)
1 题目1086 Tree Traversals Again (25分)An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys number...原创 2020-01-31 18:22:42 · 374 阅读 · 0 评论 -
1053 Path of Equal Weight (30分)
1 题目1053 Path of Equal Weight (30分)Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti . The weight of a path from R to L is defined to be the sum of the wei...原创 2020-02-01 18:59:28 · 358 阅读 · 0 评论 -
1079 Total Sales of Supply Chain (25分)
1 题目1079 Total Sales of Supply Chain (25分)A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.Star...原创 2020-02-01 21:12:57 · 767 阅读 · 0 评论 -
1090 Highest Price in Supply Chain (25分)
1 题目1090 Highest Price in Supply Chain (25分)A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.St...原创 2020-02-01 22:44:21 · 298 阅读 · 0 评论 -
1094 The Largest Generation (25分)
1 题目1094 The Largest Generation (25分)A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generati...原创 2020-02-02 08:23:15 · 347 阅读 · 0 评论 -
1102 Invert a Binary Tree (25分)
1 题目102 Invert a Binary Tree (25分)The following is from Max Howell @twitter:Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so...原创 2020-01-31 23:45:38 · 260 阅读 · 0 评论 -
1106 Lowest Price in Supply Chain (25分)
1 题目1106 Lowest Price in Supply Chain (25分)A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.Sta...原创 2020-02-01 23:57:18 · 413 阅读 · 1 评论 -
1004 Counting Leaves (30分)
1 题目1004 Counting Leaves (30分)A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.Input Specification:Each input file contains o...原创 2020-02-02 09:41:29 · 442 阅读 · 0 评论