![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构
文章平均质量分 51
水果妹妹踩前端的坑
这个作者很懒,什么都没留下…
展开
-
剑指 Offer 32 - I. 从上到下打印二叉树
剑指 Offer 32 - I. 从上到下打印二叉树题目描述:从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。例如:给定二叉树:[3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回:[3,9,20,15,7]解题思路:剑指 Offer 32 - II. 从上到下打印二叉树 II这2题就是一样的思路/** * Definition for a binary tree n...原创 2021-04-16 16:30:00 · 99 阅读 · 1 评论 -
剑指 Offer 32 - II. 从上到下打印二叉树 II
剑指 Offer 32 - II. 从上到下打印二叉树 II题目描述:从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。例如:给定二叉树:[3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回其层次遍历结果:[ [3], [9,20], [15,7]]提示:节点总数 <= 1000解题思路一:层序遍历。其实跟二叉树的深度的解题方法二,就是在...原创 2021-04-16 15:56:10 · 122 阅读 · 0 评论 -
剑指 Offer 27. 二叉树的镜像(js)
剑指 Offer 27. 二叉树的镜像题目描述:请完成一个函数,输入一个二叉树,该函数输出它的镜像。例如输入:4/ \2 7/ \ / \1 3 6 9镜像输出:4/ \7 2/ \ / \9 6 31示例 1:输入:root = [4,2,7,1,3,6,9]输出:[4,7,2,9,6,3,1]限制:0 <= 节点个数 <= 1000...原创 2021-04-15 20:12:24 · 139 阅读 · 0 评论 -
剑指 Offer 55 - I. 二叉树的深度(js实现)
剑指 Offer 55 - I. 二叉树的深度题目描述:输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点依次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。例如:给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回它的最大深度3 。解题方法一:递归方法。递归左子树和右子树的深度。当节点为null时,说明没有子节点,并返回0。/** * Definition...原创 2021-04-15 16:15:38 · 186 阅读 · 0 评论 -
剑指 Offer 59 - II. 队列的最大值
剑指 Offer 59 - II. 队列的最大值题目描述:请定义一个队列并实现函数 max_value 得到队列里的最大值,要求函数max_value、push_back 和 pop_front 的均摊时间复杂度都是O(1)。若队列为空,pop_front 和 max_value需要返回 -1示例 1:输入:["MaxQueue","push_back","push_back","max_value","pop_front","max_value"][[],[1],[2],[],..原创 2021-04-10 21:03:05 · 87 阅读 · 0 评论 -
剑指 Offer 30. 包含min函数的栈
剑指 Offer 30. 包含min函数的栈题目描述:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。示例:MinStack minStack = new MinStack();minStack.push(-2);minStack.push(0);minStack.push(-3);minStack.min(); --> 返回 -3.minStack.pop();min..原创 2021-04-10 15:09:58 · 95 阅读 · 0 评论 -
剑指 Offer 09. 用两个栈实现队列
剑指 Offer 09. 用两个栈实现队列题目描述:用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead操作返回 -1 )示例 1:输入:["CQueue","appendTail","deleteHead","deleteHead"][[],[3],[],[]]输出:[null,null,3,-1]示例 2:输入:["CQue.原创 2021-04-09 16:55:08 · 120 阅读 · 0 评论 -
1106 Lowest Price in Supply Chain (25 分)【dfs】
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.Starting from one root supplier, everyone on th...原创 2019-01-07 10:55:38 · 228 阅读 · 0 评论 -
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 s...原创 2019-01-02 20:26:06 · 284 阅读 · 0 评论 -
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 su...原创 2019-02-27 19:52:58 · 327 阅读 · 0 评论 -
PAT甲级1127 ZigZagging on a Tree (30 分)【后序中序转为先序并输出层序遍历(s型)】
1127ZigZagging 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 s...原创 2019-03-05 10:57:22 · 283 阅读 · 0 评论 -
1087 All Roads Lead to Rome (30 分)【dijkstra+dfs】
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.Input Specification:Each ...原创 2019-03-01 18:59:23 · 306 阅读 · 0 评论 -
PAT甲级1079 Total Sales of Supply Chain (25 分)【dfs】
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.Starting from one root supplier, everyone on th...原创 2019-02-26 21:20:17 · 228 阅读 · 0 评论 -
PAT甲级题型整理
按题型整理最短路径类题目:1003 Emergency (Dijkstra)1018 Public Bike Management(Dijkstra+dfs 将所有最短路径都储存在vector中)1087 All Roads Lead to Rome(Dijkstra+dfs)1111 online Map(2次Dijkstra+dfs)二叉搜索树类题目:1043 Is...原创 2019-02-21 12:57:05 · 1160 阅读 · 0 评论 -
1066 Root of AVL Tree (25 分)【套建AVL树模板】
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...原创 2019-03-28 21:30:44 · 262 阅读 · 0 评论 -
PAT甲级1018 Public Bike Management (30 分)【Dijlstra+dfs】
There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the ...原创 2019-04-02 13:55:26 · 222 阅读 · 0 评论 -
1123 Is It a Complete AVL Tree (30 分)【AVL树建立和层序遍历bfs】
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...原创 2019-03-29 11:25:25 · 278 阅读 · 0 评论 -
排序系列算法
冒泡排序它是一种较简单的排序算法。基本思想:它会遍历若干次要排序的数列,每次遍历时,它都会从前往后依次的比较相邻两个数的大小;如果前者比后者大,则交换它们的位置。这样,一次遍历之后,最大的元素就在数列的末尾! 采用相同的方法再次遍历时,第二大的元素就被排列在最大元素之前。重复此操作,直到整个数列都有序为止!每个排序都会讲思想,然后自己理解并动手敲,再看下面的代码。冒泡排序时间复杂...原创 2019-07-11 11:08:11 · 209 阅读 · 0 评论 -
zcmu-4933: 二叉排序树【二叉排序树构造,搜索,前序中序后序遍历】
4933: 二叉排序树Time Limit: 1 Sec Memory Limit: 32 MBSubmit: 5 Solved: 4[Submit][Status][Web Board]Description输入一系列整数,建立二叉排序数,并进行前序,中序,后序遍历。Input输入第一行包括一个整数n(1<=n<=100)。接下来的一行包括n个整数。O...原创 2018-08-28 20:18:43 · 551 阅读 · 0 评论 -
1155 Heap Paths (30 分)【dfs】
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 (i...原创 2019-01-02 16:52:47 · 205 阅读 · 0 评论 -
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 su...原创 2019-01-06 13:49:37 · 257 阅读 · 0 评论 -
PAT甲级--1094 The Largest Generation(25 分)【DFS+BFS+输出一层最多的结点数】
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 w...原创 2018-09-01 16:33:08 · 193 阅读 · 0 评论 -
前序中序后序转换
给出后序中序,转成前序,root根节点void pre(int root, int start, int end) { if(start > end) return ; int i = start; while(i < end && in[i] != post[root]) i++; printf("%d ", post[root]...原创 2018-08-29 10:35:52 · 730 阅读 · 0 评论 -
PAT甲级-1020 Tree Traversals(25 分)【后序中序+层序遍历】
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 order t...原创 2018-08-29 10:18:52 · 226 阅读 · 0 评论 -
PAT甲级--1102 Invert a Binary Tree(25 分)【反转二叉树+层序遍历+中序遍历】
1102 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 ...原创 2018-08-31 21:01:12 · 273 阅读 · 0 评论 -
PAT甲级--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 numb...原创 2018-08-31 09:37:39 · 204 阅读 · 0 评论 -
zcmu-4932: 树查找【完全二叉树+输出指定层数节点】
不忘初心,砥砺前行!4932: 树查找Time Limit: 1 Sec Memory Limit: 32 MBSubmit: 6 Solved: 4[Submit][Status][Web Board]Description有一棵树,输出某一深度的所有节点,有则输出这些节点,无则输出EMPTY。该树是完全二叉树。Input 输入有多组数据。每组输入一个n(1...原创 2018-08-27 16:48:51 · 1065 阅读 · 0 评论 -
zcmu-4931: 二叉树遍历【前序+中序,输出后序】
4931: 二叉树遍历Time Limit: 1 Sec Memory Limit: 32 MBSubmit: 5 Solved: 5[Submit][Status][Web Board]Description二叉树的前序、中序、后序遍历的定义:前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历其右子树;中序遍历:对任一子树,先遍历其左子树,然后访问根,最后遍历其右...原创 2018-08-27 15:44:21 · 366 阅读 · 0 评论 -
PAT甲级--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 t...原创 2018-08-27 15:34:33 · 198 阅读 · 0 评论 -
PAT甲级--1021 Deepest Root(25 分)【dfs+连通分量+STL】
1021 Deepest Root(25 分)A graph which is connected and acyclic can be considered a tree. The hight of the tree depends on the selected root. Now you are supposed to find the root that results in a hi...原创 2018-09-04 14:59:27 · 486 阅读 · 0 评论 -
PAT甲级-1051 Pop Sequence(25 分)【栈模拟删除操作】
1051 Pop Sequence(25 分)Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a p...原创 2018-09-02 20:41:29 · 202 阅读 · 0 评论 -
PAT甲级--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 ...原创 2018-09-08 19:47:27 · 221 阅读 · 0 评论 -
1090 Highest Price in Supply Chain (25 分)【dfs】
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.Starting from one root supplier, everyone on th...原创 2019-01-05 15:11:42 · 594 阅读 · 0 评论 -
1110 Complete Binary Tree (25 分)【完全二叉树(dfs)】
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) wh...原创 2019-01-04 15:51:49 · 311 阅读 · 0 评论 -
1151 LCA in a Binary Tree (30 分)【最小公共祖先】
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 a...原创 2019-01-03 15:20:37 · 753 阅读 · 4 评论 -
1064 Complete Binary Search Tree (30 分)【二叉树中序转化成层序】
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 key...原创 2018-11-16 09:54:51 · 159 阅读 · 0 评论 -
1034 Head of a Gang (30 分)【连通分量dfs变形】
One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be t...原创 2018-11-03 13:38:30 · 163 阅读 · 0 评论 -
1013 Battle Over Cities (25 分)【连通分量+dfs】
1013 Battle Over Cities (25 分)It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We...原创 2018-10-31 16:39:23 · 229 阅读 · 0 评论 -
zcmu--4934: 二叉搜索树【数组保存结点值】
4934: 二叉搜索树Time Limit: 1 Sec Memory Limit: 32 MBSubmit: 10 Solved: 5[Submit][Status][Web Board]Description判断两序列是否为同一二叉搜索树序列Input开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。接下去一行是一个序列...原创 2018-09-09 19:43:18 · 193 阅读 · 0 评论 -
PAT甲级--1032 Sharing(25 分)【链表连接】
1032 Sharing(25 分)To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same ...原创 2018-09-03 16:28:37 · 293 阅读 · 0 评论