
树
文章平均质量分 77
感觉自己就是个bug
这个作者很懒,什么都没留下…
展开
-
PAT(甲级)2024夏季考试A-4
【代码】PAT(甲级)2024夏季考试A-4。原创 2024-06-21 19:04:23 · 219 阅读 · 0 评论 -
PAT(甲级)2019年秋季考试7-3 Postfix Expression (25 分)
7-3 Postfix Expression (25 分)Given a syntax tree (binary), you are supposed to output the corresponding postfix expression, with parentheses reflecting the precedences of the operators.Input Specification:Each input file contains one test case. For each原创 2021-03-11 00:55:07 · 134 阅读 · 0 评论 -
PAT(甲级)2019年冬季考试7-4 Cartesian Tree (30 分)
7-4 Cartesian Tree (30 分)A Cartesian tree is a binary tree constructed from a sequence of distinct numbers. The tree is heap-ordered, and an inorder traversal returns the original sequence. For example, given the sequence { 8, 15, 3, 4, 1, 5, 12, 10, 18,原创 2021-03-10 14:43:49 · 190 阅读 · 0 评论 -
2020PAT甲级秋季7-3 Left-View of Binary Tree (25分)
7-3Left-View of Binary Tree(25分)The left-view of a binary tree is a list of nodes obtained by looking at the tree from left hand side and from top down. For example, given a tree shown by the figure, its left-view is { 1, 2, 3, 4, 5 }Given the inorder a原创 2021-03-08 20:09:17 · 474 阅读 · 0 评论 -
1043 Is It a Binary Search Tree (25 分)
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 keys less than the node’s key.The right subtree of a node con原创 2021-03-06 11:51:29 · 80 阅读 · 0 评论 -
1066 Root of AVL Tree (25 分)
1066 Root of AVL Tree (25 分)An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this prope原创 2021-03-04 10:37:37 · 89 阅读 · 1 评论 -
1086 Tree Traversals Again (25 分)
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 numbered from 1 to 6) is traversed, the stack operations are: push原创 2021-03-02 11:41:23 · 77 阅读 · 0 评论 -
1094 The Largest Generation (25 分)
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 generation with the largest population.Input Specification:Each in原创 2021-03-01 14:14:47 · 76 阅读 · 0 评论 -
1099 Build A Binary Search Tree (30 分)
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 keys less than the node’s key.The right subtree of a node con原创 2021-03-01 12:47:16 · 70 阅读 · 0 评论 -
1103 Integer Factorization (30 分)
1103 Integer Factorization (30 分)The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K−P factorization of N for any positive integers N, K and P.原创 2021-02-28 17:32:46 · 124 阅读 · 0 评论 -
1110 Complete Binary Tree (25 分)
1110 Complete Binary Tree (25 分)Given a tree, you are supposed to tell if it is a complete binary tree.Input Specification:Each input file contains one test case. For each case, the first line gives a positive integer N (≤20) which is the total number o原创 2021-02-28 10:44:56 · 174 阅读 · 0 评论 -
1115 Counting Nodes in a BST (30 分)
1115 Counting Nodes in a BST (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 keys less than or equal to the node’s key.The right subtree of a原创 2021-02-26 12:54:24 · 99 阅读 · 0 评论 -
1119 Pre- and Post-order Traversals (30 分)
1119 Pre- and Post-order Traversals (30 分)Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal seq原创 2021-02-26 12:55:04 · 95 阅读 · 0 评论 -
1123 Is It a Complete AVL Tree (30 分)
1123 Is It a Complete AVL Tree (30 分)An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore t原创 2021-02-25 14:05:53 · 82 阅读 · 0 评论 -
1127 ZigZagging on a Tree (30 分)
1127 ZigZagging on a Tree (30 分)Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print th原创 2021-02-25 11:49:37 · 67 阅读 · 0 评论 -
1130 Infix Expression (25 分)
1130 Infix Expression (25 分)Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with parentheses reflecting the precedences of the operators.Input Specification:Each input file contains one test case. For each ca原创 2021-02-25 10:15:03 · 124 阅读 · 0 评论 -
1135 Is It A Red-Black Tree (30 分) 红黑树
1135 Is It A Red-Black Tree (30 分)There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties:(1) Every node is either red or black.(2) The root is black.(3) Every leaf (NULL) is black.(原创 2021-02-24 14:15:49 · 397 阅读 · 0 评论 -
1138 Postorder Traversal (25 分)
1138 Postorder Traversal (25 分)Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and inorder traversal sequences, you are supposed to output the first number of the postorder traversal sequence of the correspond原创 2021-02-24 12:15:02 · 95 阅读 · 0 评论 -
1143 Lowest Common Ancestor (30 分)
1143 Lowest Common Ancestor (30 分)The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.A binary search tree (BST) is recursively defined as a binary tree which has the following properti原创 2021-02-23 12:52:05 · 75 阅读 · 0 评论 -
1146 Topological Order (25 分)
1146 Topological Order (25 分)This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topological order obtained from the given directed graph? Now you are supposed to write a program to test each of the options.Inp原创 2021-02-23 11:19:58 · 131 阅读 · 0 评论 -
1147 Heaps (30 分)
1147 Heaps (30 分)In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to原创 2021-02-23 11:00:04 · 106 阅读 · 0 评论 -
1151 LCA in a Binary Tree (30 分)
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.Given any two nodes in a binary tree, you are supposed to find their LCA.Input Specification:Each input file contains one test case.原创 2021-02-22 13:49:35 · 76 阅读 · 0 评论 -
PAT(甲级)2019年春季考试7-4 Structure of a Binary Tree (30 分)
Structure of a Binary Tree (30分)重复代码很多,可以修剪一下#include<bits/stdc++.h>using namespace std;struct Node { int left,right; int level,parent; Node() { left=right=parent=-1; }} node[1005];vector<int>in,post;void Level(int root) { queue&l原创 2020-06-12 14:55:55 · 299 阅读 · 0 评论 -
1155 Heap Paths (30分)---Heap
https://pintia.cn/problem-sets/994805342720868352/problems/10717854088490475521155 Heap Paths (30分)In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the valu原创 2020-06-12 14:42:43 · 124 阅读 · 0 评论 -
1102 Invert a Binary Tree (25分)---二叉树的层次和中序遍历
https://pintia.cn/problem-sets/994805342720868352/problems/9948053655378821121102 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原创 2020-06-07 15:48:19 · 167 阅读 · 0 评论 -
1004 Counting Leaves (30分)---BFS
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 one test case. Each case starts with a line containing 0<N&原创 2020-05-31 12:30:10 · 158 阅读 · 1 评论 -
05-树9 Huffman Codes (30分)
05-树9 Huffman Codes (30分)In 1953, David A. Huffman published his paper “A Method for the Construction of Minimum-Redundancy Codes”, and hence printed his name in the history of computer science. As a professor who gives the final exam problem on Huffman c原创 2020-05-26 13:11:42 · 218 阅读 · 0 评论 -
05-树8 File Transfer (25分)
05-树8 File Transfer (25分)We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other?I原创 2020-05-26 13:09:54 · 87 阅读 · 0 评论 -
05-树7 堆中的路径 (25分)
05-树7 堆中的路径 (25分)将一系列给定数字插入一个初始为空的小顶堆H[]。随后对任意给定的下标i,打印从H[i]到根结点的路径。输入格式:每组测试第1行包含2个正整数N和M(≤1000),分别是插入元素的个数、以及需要打印的路径条数。下一行给出区间[-10000, 10000]内的N个要被插入一个初始为空的小顶堆的整数。最后一行给出M个下标。输出格式:对输入中给出的每个下标i,在一行中输出从H[i]到根结点的路径上的数据。数字间以1个空格分隔,行末不得有多余空格。输入样例:5 346原创 2020-05-26 13:10:19 · 198 阅读 · 0 评论 -
04-树7 二叉搜索树的操作集 (30分)
04-树7 二叉搜索树的操作集 (30分)Position Find( BinTree BST, ElementType X ) { if(!BST) { return NULL; } else { while(BST) { if(BST->Data<X) BST=BST->Right; else if(BST->Data>X) { BST=BST->Left; } else return BST; } re原创 2020-05-26 13:10:37 · 225 阅读 · 0 评论 -
04-树6 Complete Binary Search Tree (30分)
04-树6 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 keys less than the node’s key.The right subtree of a node co原创 2020-05-26 13:03:15 · 197 阅读 · 0 评论 -
04-树5 Root of AVL Tree (25分)
04-树5 Root of AVL Tree (25分)An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this prope原创 2020-05-26 13:02:04 · 162 阅读 · 0 评论 -
04-树4 是否同一棵二叉搜索树 (25分)
04-树4 是否同一棵二叉搜索树 (25分)给定一个插入序列就可以唯一确定一棵二叉搜索树。然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到。例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结果。于是对于输入的各种插入序列,你需要判断它们是否能生成一样的二叉搜索树。输入格式:输入包含若干组测试数据。每组数据的第1行给出两个正整数N (≤10)和L,分别是每个序列插入元素的个数和需要检查的序列个数。第2行给出N个以空格分隔的正整数,作为初始插入序列。最后原创 2020-05-26 12:59:53 · 180 阅读 · 0 评论 -
03-树3 Tree Traversals Again (25分)
03-树3 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 numbered from 1 to 6) is traversed, the stack operations are: push原创 2020-05-26 12:58:17 · 175 阅读 · 0 评论 -
03-树2 List Leaves (25分)
03-树2 List Leaves (25分)Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.Input Specification:Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is原创 2020-05-26 12:56:45 · 187 阅读 · 0 评论 -
03-树1 树的同构 (25分)
03-树1 树的同构 (25分)给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。图1图2现给定两棵树,请你判断它们是否是同构的。输入格式:输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号);随后N行,第i行对应编号第i个结点,给出该结点中存储的原创 2020-05-26 12:52:29 · 319 阅读 · 0 评论 -
二叉树的遍历操作
二叉树的遍历操作#include<iostream>#include<stack>#include<queue> using namespace std;typedef int ElementType;typedef struct TNode* Position;typedef Position BinTree;struct TNode { ElementType Data; BinTree Left; BinTree Right;};//1原创 2020-05-26 13:55:25 · 165 阅读 · 0 评论