![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
文章平均质量分 70
boligongzhu
这个作者很懒,什么都没留下…
展开
-
1025. Divisor Game
思路:看到题目限制1原创 2023-04-14 12:07:19 · 500 阅读 · 0 评论 -
652. Find Duplicate Subtrees
思路:最暴力的办法是遍历整棵树,每遇到一个节点重新遍历树,寻找是否有与其相同的子树结构。时间复杂度是O(n^3)。因此,为简化时间复杂度,可以在遍历时将其转化成字符串数组保存起来,每次遍历到新节点只需比较是否有与其相同的字符串即可。时间复杂度简化为O(n^2)。用hashmap来保存字符串,value的值记录字符串出现的次数,只有曾经出现一次时才将子树根节点的指针保存在结果中,避免具有3个或3个以上相同子树结构时结果重复。题目:给定一个二叉树,求里面完全相同的子树。返回子树根节点的集合。原创 2022-10-09 11:19:51 · 315 阅读 · 0 评论 -
863. All Nodes Distance K in Binary Tree
思路:首先需要找到给定的节点位置。距离给定节点target 为k的节点可能在target之下(子孙辈节点),也可能在target之上(祖辈节点),也可能在target旁边。因此需要两种操作,一种向下寻找距离给定节点为k,一种向上寻找。向下寻找比较简单,只找到距离为k的节点并放入结果中即可。向上寻找需要逐层寻找,比如向上一层找到target的parent后,需要再找到旁支与partent距离为k-1的所有节点。题目: 给定一个二叉树,和其中一个节点。求与给定节点距离为k的所有节点的值。路径不一定要过根节点。原创 2022-09-16 15:22:01 · 587 阅读 · 0 评论 -
894. All Possible Full Binary Trees
采用动态规划方法,将n-1个节点分为左右子节点。采用递归方法分别求出从1到n-2个节点的完全二叉树结构,将其置于根节点的左右子树位置。小编使用一个vector subTree结构来代替hashmap,subTree[i]即为包含i个节点的所有完全二叉树结构。为保证树结构为完全二叉树,i为奇数。题目:给定正整数n。求包含n个节点的所有完全二叉树的结构。每个节点值为0. 完全二叉树是一个二叉树,其所有节点包含0个或2个子节点。原创 2022-09-16 12:51:34 · 267 阅读 · 0 评论 -
1530. Number of Good Leaf Nodes Pairs
思路:叶子不可能直接与叶子相通,必然是通过一个共同的父结点或祖辈节点,需要记录以每个节点作为根节点的子树下有几个叶子节点,且叶子节点离当前子根节点的距离。可递归为计算其左节点下的叶子节点和其右节点下的叶子节点,计算其所有左叶子节点和所有右叶子节点的距离(为其左叶子节点距离当前节点的距离+右叶子节点距离当前节点的距离),如距离小于给定distance,则更新结果。用hashmap记录。注:hasmap的key记录当前子节点所有叶子节点的距离,value记录某一距离的叶子节点的个数。原创 2022-09-07 11:04:56 · 264 阅读 · 0 评论 -
655. Print Binary Tree
思路:先判断二叉树的深度,以此生成一个空的字符串数组。数组行数为二叉树的层数,列数为pow(2, height) -1。用队列来记录一个满二叉树,空着的地方用NULL表示。题目:给定一棵二叉树,生成一个字符串数组。原创 2022-09-07 10:53:00 · 177 阅读 · 0 评论 -
1372. Longest ZigZag Path in a Binary Tree
思路:递归,zigzag路不一定从根节点开始,因此需要记录以每个节点开始的最长zigzag路径,从底部往上回溯。因此从上往下时需要用direction(dir)记录每个节点是左节点还是右节点。如果是左节点(-1代表左节点),则从底部往上时需选择右子节点的zigzag路,如果是右节点(1代表右节点),则需选择其左子节点的zigzag路。根节点用0代表,则选择左子节点或右子节点最长的那条路。题目:给定一棵二叉树,找其中最长的zigzag路。原创 2022-09-02 09:10:04 · 527 阅读 · 0 评论 -
96. Unique Binary Search Trees
思路:由于是二叉搜索树,因此只要根节点确定了,左右子树节点个数也就确定。从1到n遍历,分别从1到n作为根节点,求左右子树的结构个数相乘。最后的加和即为结果。为减少重复运算,用vector数组dp记录i个节点的结构数。题目:给一个正整数n,求由节点1~n组成的二叉搜索树结构有几个。...原创 2022-09-01 15:54:25 · 162 阅读 · 0 评论 -
508. Most Frequent Subtree Sum
题目:给定一个二叉树,找出所有出现次数最多的子树和的值。原创 2022-08-17 13:15:48 · 213 阅读 · 0 评论 -
919. Complete Binary Tree Inserter
思路:容易错的点是初始化时不只是一个根节点,可能已经是一棵树。用queue来保存所有不完全的子节点。题目:实现一个类,可以往二叉树中插入节点,使得二叉树保持完全二叉树。原创 2022-08-17 13:04:23 · 143 阅读 · 0 评论 -
1325. Delete Leaves With a Given Value
题目:给定一个二叉树,将与给定target值相等的叶结点删除。注意,删除之后其父结点可能变为叶结点。思路:一看就是递归,从下网上删就完了。原创 2022-08-17 12:26:46 · 191 阅读 · 0 评论 -
998. Maximum Binary Tree II
思路:因为val是在构建二叉树的数组最后,因此在二叉树中往右子树找,找到最后一个比val大的节点。将其右子树改为新建节点的左子树,新建节点为其右子节点。题目:创建最大树,给定一颗最大二叉树,和一个值val, 将val以最右的形式插入二叉树。...原创 2022-08-17 11:55:21 · 205 阅读 · 0 评论 -
669. Trim a Binary Search Tree
题目:给二叉搜索树剪枝,使得所有节点值都在[low, high]之间。原创 2022-08-17 11:25:09 · 219 阅读 · 0 评论 -
1457. Pseudo-Palindromic Paths in a Binary Tree
思路:由于节点是由1~9的数字组成,用一个长度为10的vector保存每条路个数字出现的次数。题目:给定一个二叉树,每个节点都是由1~9的数字组成,问能组成伪回文的线路有几条。原创 2022-08-17 10:56:44 · 215 阅读 · 0 评论 -
865. Smallest Subtree with all the Deepest Nodes
与 相同。原创 2022-08-12 12:16:19 · 125 阅读 · 0 评论 -
230. Kth Smallest Element in a BST
题目:给定一个二叉查找树,返回第K小的值。思路:树的inorder遍历的变体。原创 2022-08-12 12:05:29 · 182 阅读 · 0 评论 -
1110. Delete Nodes And Return Forest
题目: 给定一棵二叉树,和一个数组。删除树中值等于数组元素值的节点。用queue来保存所有节点。原创 2022-08-12 11:50:22 · 336 阅读 · 0 评论 -
1123. Lowest Common Ancestor of Deepest Leaves
思路:需要有一个全局变量记录整个子树的最深深度,此变量再遍历时是实时更新的。同时需要局部变量记录以当前子节点为根的子树所包含叶子的深度。如果当前节点左子树与右子树深度相同,且同为最深深度,则该子节点即为我们所求。因此,单次遍历,通过递归所隐含的栈结构来记录从上往下遍历树时的深度,从下往上时回传当前节点子辈的最深深度。判断当前节点左右子辈的最深深度是否相等,且是否等于整棵树的最深深度。题目:给定一个二叉树,找 最深两个叶子的最低公共祖先。......原创 2022-08-12 10:46:29 · 367 阅读 · 0 评论 -
1305. All Elements in Two Binary Search Trees
思路:inorder traversal的变体。两个二叉搜索树一起inorder遍历。题目:将两个二叉搜索树的元素按从小到大排列。原创 2022-08-05 11:43:11 · 74 阅读 · 0 评论 -
1008. Construct Binary Search Tree from Preorder Traversal
思路很像,用栈来保存每个节点,不同的是,本题是以大小来判断左右子树,而654是以位置来判断左右子树。题目:给定一个二叉查找树的preorder排序数组,让还原出这个二叉查找树。时间:O(N), 空间:O(N)...原创 2022-08-05 10:34:23 · 158 阅读 · 0 评论 -
654. Maximum Binary Tree
方法二,因为数组中每个值都会是树中的一个节点,因此从头往后遍历,每个值都生成一个树节点。用一个栈维护着树结构,栈中树节点的值是单调递减的。如遍历到的值比栈顶节点值小,直接加在栈顶节点的右子树上。如果遍历到的值比栈顶节点大,则代表新遍历到的值是栈顶节点的父辈或祖辈~。pop出的节点位于新节点的左子树上。要求树顶位置的值大于左右子树的值,而且左右子树也符合这个规则。时间复杂度:O(N^2), 空间复杂度:递归用到的栈空间不算的话,为O(1)方法一,当然是笨方法,递归,每次找数组中的最大值就好了。...原创 2022-08-05 10:07:18 · 109 阅读 · 0 评论 -
144. Binary Tree Preorder Traversal
方法二,stack迭代法。原创 2022-07-19 12:03:10 · 151 阅读 · 0 评论 -
145. Binary Tree Postorder Traversal
方法一stack迭代法。原创 2022-07-19 11:52:33 · 145 阅读 · 0 评论 -
94. Binary Tree Inorder Traversal
方法一,stack迭代法。原创 2022-07-19 11:36:18 · 73 阅读 · 0 评论 -
2193. Minimum Number of Moves to Make Palindrome
思路双指针+贪心算法。指针i,j最开始指向首位和尾位,如果i位和j位相等,则直接i++,j--。如果不相等,则一直从前找到与s[j]相等和从后找到与s[i]相等的位置。比较哪边需要挪动的步骤少则挪动哪步。题目给定一串字符串,每步两个相邻的字符可以呼唤,问最少换多少步可以将字符串变成回文。假设字符串肯定可以变成回文。...原创 2022-07-18 11:39:02 · 134 阅读 · 0 评论 -
1537. Get the Maximum Score
直接找两个数组每相邻两个相等值之间的和。结果加上最大和即可。数组是严格递增的,因此不存在相等的情况,使得题目简单多了。题目给定两个数组,从前往后遍历,在数组值相等时可以交换数组遍历。遍历过的值相加,问最后最大值路径;...原创 2022-07-15 11:18:29 · 128 阅读 · 0 评论 -
1793. Maximum Score of a Good Subarray
思路双指针,难点在如果维护指针之间的最小值,使得不用每次遍历i到j,能不能每次挪动指针的时候用时间复杂度O(1)来确定i到j之间的最小值呢?因为题目有约束i=k。因为指针i,j移动的话,如果仍然包含最小值,min(nums[i]...nums[j])不变,而(j-i+1)变小了,是不会找到比当前值大的结果的。题目给定数组,找到一组子序列(i~j),其中i...原创 2022-07-15 10:52:28 · 234 阅读 · 0 评论 -
1147. Longest Chunked Palindrome Decomposition
You are given a string . You should split it to k substrings such that:Return the largest possible value of .Example 1:Input: text = "ghiabcdefhelloadamhelloabcdefghi"Output: 7Explanation: We can split the string on "(ghi)(abcdef)(hello)原创 2022-07-12 11:19:13 · 76 阅读 · 0 评论 -
2300. Successful Pairs of Spells and Potions
You are given two positive integer arrays and , of length and respectively, where represents the strength of the spell and represents the strength of the potion.You are also given an integer . A spell and potion pair is considered successful if the原创 2022-07-12 11:00:28 · 392 阅读 · 0 评论 -
457. Circular Array Loop
You are playing a game involving a circular array of non-zero integers . Each denotes the number of indices forward/backward you must move if you are located at index :Since the array is circular, you may assume that moving forward from the last element p原创 2022-07-12 10:37:35 · 103 阅读 · 0 评论 -
1712. Ways to Split Array Into Three Subarrays
A split of an integer array is good if:Given , an array of non-negative integers, return the number of good ways to split . As the number may be too large, return it modulo .Example 1:Input: nums = [1,1,1]Output: 1Explanation: The only good way to split原创 2022-07-12 09:47:44 · 188 阅读 · 0 评论 -
1616. Split Two Strings to Make Palindrome
You are given two strings and of the same length. Choose an index and split both strings at the same index, splitting into two strings: and where , and splitting into two strings: and where . Check if or forms a palindrome.When you split a string原创 2022-07-11 13:05:30 · 121 阅读 · 0 评论 -
1871. Jump Game VII
You are given a 0-indexed binary string and two integers and . In the beginning, you are standing at index , which is equal to . You can move from index to index if the following conditions are fulfilled:Return if you can reach index in , or otherwi原创 2022-07-11 12:41:44 · 101 阅读 · 0 评论 -
1813. Sentence Similarity III
A sentence is a list of words that are separated by a single space with no leading or trailing spaces. For example, , , are all sentences. Words consist of only uppercase and lowercase English letters.Two sentences and are similar if it is possible to i原创 2022-07-11 11:03:27 · 97 阅读 · 0 评论 -
556. Next Greater Element III
Given a positive integer , find the smallest integer which has exactly the same digits existing in the integer and is greater in value than . If no such positive integer exists, return .Note that the returned integer should fit in 32-bit integer, if there原创 2022-07-11 09:43:50 · 134 阅读 · 0 评论 -
475. Heaters
Winter is coming! During the contest, your first job is to design a standard heater with a fixed warm radius to warm all the houses.Every house can be warmed, as long as the house is within the heater's warm radius range. Given the positions of and on a原创 2022-07-08 13:21:25 · 70 阅读 · 0 评论 -
633. Sum of Square Numbers
Given a non-negative integer , decide whether there're two integers and such that .Example 1:Input: c = 5Output: trueExplanation: 1 * 1 + 2 * 2 = 5Example 2:Input: c = 3Output: falseConstraints:time: O(sqrt(c)), space: O(1)原创 2022-07-08 11:56:31 · 302 阅读 · 0 评论 -
151. Reverse Words in a String
Given an input string , reverse the order of the words.A word is defined as a sequence of non-space characters. The words in will be separated by at least one space.Return a string of the words in reverse order concatenated by a single space.Note that ma原创 2022-07-08 11:43:59 · 394 阅读 · 0 评论 -
948. Bag of Tokens
You have an initial power of , an initial score of , and a bag of where is the value of the token (0-indexed).Your goal is to maximize your total score by potentially playing each token in one of two ways:Each token may be played at most once and in any原创 2022-07-08 11:23:55 · 1680 阅读 · 0 评论 -
1574. Shortest Subarray to be Removed to Make Array Sorted
Given an integer array , remove a subarray (can be empty) from such that the remaining elements in are non-decreasing.Return the length of the shortest subarray to remove.A subarray is a contiguous subsequence of the array.Example 1:Input: arr = [1,2,3,原创 2022-07-08 10:54:29 · 225 阅读 · 0 评论