-
828. Count Unique Characters of All Substrings of a Given String
-
1269. Number of Ways to Stay in the Same Place After Some Steps
-
1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance
-
1420. Build Array Where You Can Find The Maximum Exactly K Comparisons
-
1449. Form Largest Integer With Digits That Add up to Target
-
1467. Probability of a Two Boxes Having The Same Number of Distinct Balls
-
1477. Find Two Non-overlapping Sub-arrays Each With Target Sum
-
1526. Minimum Number of Increments on Subarrays to Form a Target Array
-
1639. Number of Ways to Form a Target String Given a Dictionary
-
1770. Maximum Score from Performing Multiplication Operations
-
1866. Number of Ways to Rearrange Sticks With K Sticks Visible
-
1888. Minimum Number of Flips to Make the Binary String Alternating
-
1981. Minimize the Difference Between Target and Chosen Elements
-
2002. Maximum Product of the Length of Two Palindromic Subsequences
-
2035. Partition Array Into Two Arrays to Minimize Sum Difference
-
2060. Check if an Original String Exists Given Two Encoded Strings
-
2167. Minimum Time to Remove All Cars Containing Illegal Goods
-
2400. Number of Ways to Reach a Position After Exactly k Steps
-
2436. Minimum Split Into Subarrays With GCD Greater Than One
-
2472. Maximum Number of Non-overlapping Palindrome Substrings
-
2510. Check if There is a Path With Equal Number of 0’s And 1’s
-
2522. Partition String Into Substrings With Values at Most K
-
2556. Disconnect Path in a Binary Matrix by at Most One Flip
-
2892. Minimizing Array After Replacing Pairs With Their Product
-
2930. Number of Strings Which Can Be Rearranged to Contain Substring
-
3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K
-
3018. Maximum Number of Removal Queries That Can Be Processed I
-
3041. Maximize Consecutive Elements in an Array After Modification
-
3135. Equalize Strings by Adding or Removing Characters at Ends
-
3144. Minimum Substring Partition of Equal Character Frequency
-
3192. Minimum Operations to Make Binary Array Elements Equal to One II
-
3389. Minimum Operations to Make Character Frequencies Equal
-
3409. Longest Subsequence With Decreasing Adjacent Difference
-
3410. Maximize Subarray Sum After Removing All Occurrences of One Element
-
3428. Maximum and Minimum Sums of at Most Size K Subsequences
-
3472. Longest Palindromic Subsequence After at Most K Operations
-
3505. Minimum Operations to Make Elements Within K Subarrays Equal
树形 DP 套路使用前提:如果题目求解目标是 S 规则,则求解释流程可以定成以每一个节点为头节点的子树在 S 规则下的每一个答案,并且最终答案一定就在其中。
树形 DP 套路的解题步骤
-
第一步:以某个节点 X 为头节点的子树中,分析答案有哪些可能性,并且这种分析是以 X 的左子树、X 的右子树和 X 整棵树的角度来考虑可能性的。
Tip请注意这里的顺序:左子树、右子树及整棵树。先左右,如果左右满足,则就可以先上延伸,判断出整棵树。这也是递归调用“触底反弹”的过程。所以,很容易使用递归来完成相关操作。根据上述的流程,使用 后序遍历 更合适。 -
第二步:根据第一步的可能性分析,列出所有需要的信息。比如:最大值、最小值,高度,深度,节点数等等。
-
第三步:合并第二步的信息,对左树和右树提出同样的要求,并写出信息结构。
Tip写出信息结构是把所有的信息都装到一个对象中。如果只需要一个信息,就可以用简单类型来表示了。但是,在树的结构中,大概率是需要多个信息的。 -
第四步:设计递归函数,递归函数是处理以 X 为头节点的情况下的大难,包括设计递归的 base case,默认直接得到左树和右树的所有信息,以及把可能性做整合,并且要返回第三步的信息结构。