bowang0911/leetcode huggingface dataset:
Continuous Subarray Sum

Given an integer array nums and an integer k, return `true` _if_ `nums` _has a **good subarray** or_ `false` _otherwise_.

A **good subarray** is a subarray where:

*   its length is **at least two**, and
*   the sum of the elements of the subarray is a multiple of `k`.

**Note** that:

*   A **subarray** is a contiguous part of the array.
*   An integer `x` is a multiple of `k` if there exists an integer `n` such that `x = n * k`. `0` is **always** a multiple of `k`.

**Example 1:**

**Input:** nums = \[23,2,4,6,7\], k = 6
**Output:** true
**Explanation:** \[2, 4\] is a continuous subarray of size 2 whose elements sum up to 6.

**Example 2:**

**Input:** nums = \[23,2,6,4,7\], k = 6
**Output:** true
**Explanation:** \[23, 2, 6, 4, 7\] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 \* 6 and 7 is an integer.

**Example 3:**

**Input:** nums = \[23,2,6,4,7\], k = 13
**Output:** false

**Constraints:**

*   `1 <= nums.length <= 105`
*   `0 <= nums[i] <= 109`
*   `0 <= sum(nums[i]) <= 231 - 1`
*   `1 <= k <= 231 - 1`
Longest Word in Dictionary through Deleting

Given a string `s` and a string array `dictionary`, return _the longest string in the dictionary that can be formed by deleting some of the given string characters_. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.

**Example 1:**

**Input:** s =  "abpcplea ", dictionary = \[ "ale ", "apple ", "monkey ", "plea "\]
**Output:**  "apple "

**Example 2:**

**Input:** s =  "abpcplea ", dictionary = \[ "a ", "b ", "c "\]
**Output:**  "a "

**Constraints:**

*   `1 <= s.length <= 1000`
*   `1 <= dictionary.length <= 1000`
*   `1 <= dictionary[i].length <= 1000`
*   `s` and `dictionary[i]` consist of lowercase English letters.
Contiguous Array

Given a binary array `nums`, return _the maximum length of a contiguous subarray with an equal number of_ `0` _and_ `1`.

**Example 1:**

**Input:** nums = \[0,1\]
**Output:** 2
**Explanation:** \[0, 1\] is the longest contiguous subarray with an equal number of 0 and 1.

**Example 2:**

**Input:** nums = \[0,1,0\]
**Output:** 2
**Explanation:** \[0, 1\] (or \[1, 0\]) is a longest contiguous subarray with equal number of 0 and 1.

**Constraints:**

*   `1 <= nums.length <= 105`
*   `nums[i]` is either `0` or `1`.
Beautiful Arrangement

Suppose you have `n` integers labeled `1` through `n`. A permutation of those `n` integers `perm` (**1-indexed**) is considered a **beautiful arrangement** if for every `i` (`1 <= i <= n`), **either** of the following is true:

*   `perm[i]` is divisible by `i`.
*   `i` is divisible by `perm[i]`.

Given an integer `n`, return _the **number** of the **beautiful arrangements** that you can construct_.

**Example 1:**

**Input:** n = 2
**Output:** 2
**Explanation:** 
The first beautiful arrangement is \[1,2\]:
    - perm\[1\] = 1 is divisible by i = 1
    - perm\[2\] = 2 is divisible by i = 2
The second beautiful arrangement is \[2,1\]:
    - perm\[1\] = 2 is divisible by i = 1
    - i = 2 is divisible by perm\[2\] = 1

**Example 2:**

**Input:** n = 1
**Output:** 1

**Constraints:**

*   `1 <= n <= 15`
Word Abbreviation

Given an array of **distinct** strings `words`, return _the minimal possible **abbreviations** for every word_.

The following are the rules for a string abbreviation:

1.  The **initial** abbreviation for each word is: the first character, then the number of characters in between, followed by the last character.
2.  If more than one word shares the **same** abbreviation, then perform the following operation:
    *   **Increase** the prefix (characters in the first part) of each of their abbreviations by `1`.
        *   For example, say you start with the words `[ "abcdef ", "abndef "]` both initially abbreviated as `"a4f "`. Then, a sequence of operations would be `[ "a4f ", "a4f "]` -> `[ "ab3f ", "ab3f "]` -> `[ "abc2f ", "abn2f "]`.
    *   This operation is repeated until every abbreviation is **unique**.
3.  At the end, if an abbreviation did not make a word shorter, then keep it as the original word.

**Example 1:**

**Input:** words = \["like","god","internal","me","internet","interval","intension","face","intrusion"\]
**Output:** \["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"\]

**Example 2:**

**Input:** words = \["aa","aaa"\]
**Output:** \["aa","aaa"\]

**Constraints:**

*   `1 <= words.length <= 400`
*   `2 <= words[i].length <= 400`
*   `words[i]` consists of lowercase English letters.
*   All the strings of `words` are **unique**.
Random Pick with Weight

You are given a **0-indexed** array of positive integers `w` where `w[i]` describes the **weight** of the `ith` index.

You need to implement the function `pickIndex()`, which **randomly** picks an index in the range `[0, w.length - 1]` (**inclusive**) and returns it. The **probability** of picking an index `i` is `w[i] / sum(w)`.

*   For example, if `w = [1, 3]`, the probability of picking index `0` is `1 / (1 + 3) = 0.25` (i.e., `25%`), and the probability of picking index `1` is `3 / (1 + 3) = 0.75` (i.e., `75%`).

**Example 1:**

**Input**
\[ "Solution ", "pickIndex "\]
\[\[\[1\]\],\[\]\]
**Output**
\[null,0\]

**Explanation**
Solution solution = new Solution(\[1\]);
solution.pickIndex(); // return 0. The only option is to return 0 since there is only one element in w.

**Example 2:**

**Input**
\[ "Solution ", "pickIndex ", "pickIndex ", "pickIndex ", "pickIndex ", "pickIndex "\]
\[\[\[1,3\]\],\[\],\[\],\[\],\[\],\[\]\]
**Output**
\[null,1,1,1,1,0\]

**Explanation**
Solution solution = new Solution(\[1, 3\]);
solution.pickIndex(); // return 1. It is returning the second element (index = 1) that has a probability of 3/4.
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 1
solution.pickIndex(); // return 0. It is returning the first element (index = 0) that has a probability of 1/4.

Since this is a randomization problem, multiple answers are allowed.
All of the following outputs can be considered correct:
\[null,1,1,1,1,0\]
\[null,1,1,1,1,1\]
\[null,1,1,1,0,0\]
\[null,1,1,1,0,1\]
\[null,1,0,1,0,0\]
......
and so on.

**Constraints:**

*   `1 <= w.length <= 104`
*   `1 <= w[i] <= 105`
*   `pickIndex` will be called at most `104` times.
Minesweeper

Let's play the minesweeper game ([Wikipedia](https://en.wikipedia.org/wiki/Minesweeper_(video_game)), [online game](http://minesweeperonline.com))!

You are given an `m x n` char matrix `board` representing the game board where:

*   `'M'` represents an unrevealed mine,
*   `'E'` represents an unrevealed empty square,
*   `'B'` represents a revealed blank square that has no adjacent mines (i.e., above, below, left, right, and all 4 diagonals),
*   digit (`'1'` to `'8'`) represents how many mines are adjacent to this revealed square, and
*   `'X'` represents a revealed mine.

You are also given an integer array `click` where `click = [clickr, clickc]` represents the next click position among all the unrevealed squares (`'M'` or `'E'`).

Return _the board after revealing this position according to the following rules_:

1.  If a mine `'M'` is revealed, then the game is over. You should change it to `'X'`.
2.  If an empty square `'E'` with no adjacent mines is revealed, then change it to a revealed blank `'B'` and all of its adjacent unrevealed squares should be revealed recursively.
3.  If an empty square `'E'` with at least one adjacent mine is revealed, then change it to a digit (`'1'` to `'8'`) representing the number of adjacent mines.
4.  Return the board when no more squares will be revealed.

**Example 1:**

**Input:** board = \[\[ "E ", "E ", "E ", "E ", "E "\],\[ "E ", "E ", "M ", "E ", "E "\],\[ "E ", "E ", "E ", "E ", "E "\],\[ "E ", "E ", "E ", "E ", "E "\]\], click = \[3,0\]
**Output:** \[\[ "B ", "1 ", "E ", "1 ", "B "\],\[ "B ", "1 ", "M ", "1 ", "B "\],\[ "B ", "1 ", "1 ", "1 ", "B "\],\[ "B ", "B ", "B ", "B ", "B "\]\]

**Example 2:**

**Input:** board = \[\[ "B ", "1 ", "E ", "1 ", "B "\],\[ "B ", "1 ", "M ", "1 ", "B "\],\[ "B ", "1 ", "1 ", "1 ", "B "\],\[ "B ", "B ", "B ", "B ", "B "\]\], click = \[1,2\]
**Output:** \[\[ "B ", "1 ", "E ", "1 ", "B "\],\[ "B ", "1 ", "X ", "1 ", "B "\],\[ "B ", "1 ", "1 ", "1 ", "B "\],\[ "B ", "B ", "B ", "B ", "B "\]\]

**Constraints:**

*   `m == board.length`
*   `n == board[i].length`
*   `1 <= m, n <= 50`
*   `board[i][j]` is either `'M'`, `'E'`, `'B'`, or a digit from `'1'` to `'8'`.
*   `click.length == 2`
*   `0 <= clickr < m`
*   `0 <= clickc < n`
*   `board[clickr][clickc]` is either `'M'` or `'E'`.
Minimum Absolute Difference in BST

Given the `root` of a Binary Search Tree (BST), return _the minimum absolute difference between the values of any two different nodes in the tree_.

**Example 1:**

**Input:** root = \[4,2,6,1,3\]
**Output:** 1

**Example 2:**

**Input:** root = \[1,0,48,null,null,12,49\]
**Output:** 1

**Constraints:**

*   The number of nodes in the tree is in the range `[2, 104]`.
*   `0 <= Node.val <= 105`

**Note:** This question is the same as 783: [https://leetcode.com/problems/minimum-distance-between-bst-nodes/](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)
Lonely Pixel I

Given an `m x n` `picture` consisting of black `'B'` and white `'W'` pixels, return _the number of **black** lonely pixels_.

A black lonely pixel is a character `'B'` that located at a specific position where the same row and same column don't have **any other** black pixels.

**Example 1:**

**Input:** picture = \[\[ "W ", "W ", "B "\],\[ "W ", "B ", "W "\],\[ "B ", "W ", "W "\]\]
**Output:** 3
**Explanation:** All the three 'B's are black lonely pixels.

**Example 2:**

**Input:** picture = \[\[ "B ", "B ", "B "\],\[ "B ", "B ", "W "\],\[ "B ", "B ", "B "\]\]
**Output:** 0

**Constraints:**

*   `m == picture.length`
*   `n == picture[i].length`
*   `1 <= m, n <= 500`
*   `picture[i][j]` is `'W'` or `'B'`.
K-diff Pairs in an Array

Given an array of integers `nums` and an integer `k`, return _the number of **unique** k-diff pairs in the array_.

A **k-diff** pair is an integer pair `(nums[i], nums[j])`, where the following are true:

*   `0 <= i, j < nums.length`
*   `i != j`
*   `nums[i] - nums[j] == k`

**Notice** that `|val|` denotes the absolute value of `val`.

**Example 1:**

**Input:** nums = \[3,1,4,1,5\], k = 2
**Output:** 2
**Explanation:** There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of **unique** pairs.

**Example 2:**

**Input:** nums = \[1,2,3,4,5\], k = 1
**Output:** 4
**Explanation:** There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).

**Example 3:**

**Input:** nums = \[1,3,1,5,4\], k = 0
**Output:** 1
**Explanation:** There is one 0-diff pair in the array, (1, 1).

**Constraints:**

*   `1 <= nums.length <= 104`
*   `-107 <= nums[i] <= 107`
*   `0 <= k <= 107`
Lonely Pixel II

Given an `m x n` `picture` consisting of black `'B'` and white `'W'` pixels and an integer target, return _the number of **black** lonely pixels_.

A black lonely pixel is a character `'B'` that located at a specific position `(r, c)` where:

*   Row `r` and column `c` both contain exactly `target` black pixels.
*   For all rows that have a black pixel at column `c`, they should be exactly the same as row `r`.

**Example 1:**

**Input:** picture = \[\[ "W ", "B ", "W ", "B ", "B ", "W "\],\[ "W ", "B ", "W ", "B ", "B ", "W "\],\[ "W ", "B ", "W ", "B ", "B ", "W "\],\[ "W ", "W ", "B ", "W ", "B ", "W "\]\], target = 3
**Output:** 6
**Explanation:** All the green 'B' are the black pixels we need (all 'B's at column 1 and 3).
Take 'B' at row r = 0 and column c = 1 as an example:
 - Rule 1, row r = 0 and column c = 1 both have exactly target = 3 black pixels. 
 - Rule 2, the rows have black pixel at column c = 1 are row 0, row 1 and row 2. They are exactly the same as row r = 0.

**Example 2:**

**Input:** picture = \[\[ "W ", "W ", "B "\],\[ "W ", "W ", "B "\],\[ "W ", "W ", "B "\]\], target = 1
**Output:** 0

**Constraints:**

*   `m == picture.length`
*   `n == picture[i].length`
*   `1 <= m, n <= 200`
*   `picture[i][j]` is `'W'` or `'B'`.
*   `1 <= target <= min(m, n)`
Encode and Decode TinyURL

> Note: This is a companion problem to the [System Design](https://leetcode.com/discuss/interview-question/system-design/) problem: [Design TinyURL](https://leetcode.com/discuss/interview-question/124658/Design-a-URL-Shortener-(-TinyURL-)-System/).

TinyURL is a URL shortening service where you enter a URL such as `https://leetcode.com/problems/design-tinyurl` and it returns a short URL such as `http://tinyurl.com/4e9iAk`. Design a class to encode a URL and decode a tiny URL.

There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.

Implement the `Solution` class:

*   `Solution()` Initializes the object of the system.
*   `String encode(String longUrl)` Returns a tiny URL for the given `longUrl`.
*   `String decode(String shortUrl)` Returns the original long URL for the given `shortUrl`. It is guaranteed that the given `shortUrl` was encoded by the same object.

**Example 1:**

**Input:** url =  "https://leetcode.com/problems/design-tinyurl "
**Output:**  "https://leetcode.com/problems/design-tinyurl "

**Explanation:**
Solution obj = new Solution();
string tiny = obj.encode(url); // returns the encoded tiny url.
string ans = obj.decode(tiny); // returns the original url after decoding it.

**Constraints:**

*   `1 <= url.length <= 104`
*   `url` is guranteed to be a valid URL.
Construct Binary Tree from String

You need to construct a binary tree from a string consisting of parenthesis and integers.

The whole input represents a binary tree. It contains an integer followed by zero, one or two pairs of parenthesis. The integer represents the root's value and a pair of parenthesis contains a child binary tree with the same structure.

You always start to construct the **left** child node of the parent first if it exists.

**Example 1:**

**Input:** s =  "4(2(3)(1))(6(5)) "
**Output:** \[4,2,6,3,1,5\]

**Example 2:**

**Input:** s =  "4(2(3)(1))(6(5)(7)) "
**Output:** \[4,2,6,3,1,5,7\]

**Example 3:**

**Input:** s =  "-4(2(3)(1))(6(5)(7)) "
**Output:** \[-4,2,6,3,1,5,7\]

**Constraints:**

*   `0 <= s.length <= 3 * 104`
*   `s` consists of digits, `'('`, `')'`, and `'-'` only.
Complex Number Multiplication

A [complex number](https://en.wikipedia.org/wiki/Complex_number) can be represented as a string on the form `"**real**+**imaginary**i "` where:

*   `real` is the real part and is an integer in the range `[-100, 100]`.
*   `imaginary` is the imaginary part and is an integer in the range `[-100, 100]`.
*   `i2 == -1`.

Given two complex numbers `num1` and `num2` as strings, return _a string of the complex number that represents their multiplications_.

**Example 1:**

**Input:** num1 =  "1+1i ", num2 =  "1+1i "
**Output:**  "0+2i "
**Explanation:** (1 + i) \* (1 + i) = 1 + i2 + 2 \* i = 2i, and you need convert it to the form of 0+2i.

**Example 2:**

**Input:** num1 =  "1+-1i ", num2 =  "1+-1i "
**Output:**  "0+-2i "
**Explanation:** (1 - i) \* (1 - i) = 1 + i2 - 2 \* i = -2i, and you need convert it to the form of 0+-2i.

**Constraints:**

*   `num1` and `num2` are valid complex numbers.
Convert BST to Greater Tree

Given the `root` of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.

As a reminder, a _binary search tree_ is a tree that satisfies these constraints:

*   The left subtree of a node contains only nodes with keys **less than** the node's key.
*   The right subtree of a node contains only nodes with keys **greater than** the node's key.
*   Both the left and right subtrees must also be binary search trees.

**Example 1:**

**Input:** root = \[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8\]
**Output:** \[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8\]

**Example 2:**

**Input:** root = \[0,null,1\]
**Output:** \[1,null,1\]

**Constraints:**

*   The number of nodes in the tree is in the range `[0, 104]`.
*   `-104 <= Node.val <= 104`
*   All the values in the tree are **unique**.
*   `root` is guaranteed to be a valid binary search tree.

**Note:** This question is the same as 1038: [https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/](https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/)
Minimum Time Difference

Given a list of 24-hour clock time points in **"HH:MM "** format, return _the minimum **minutes** difference between any two time-points in the list_.

**Example 1:**

**Input:** timePoints = \["23:59","00:00"\]
**Output:** 1

**Example 2:**

**Input:** timePoints = \["00:00","23:59","00:00"\]
**Output:** 0

**Constraints:**

*   `2 <= timePoints.length <= 2 * 104`
*   `timePoints[i]` is in the format **"HH:MM "**.
Single Element in a Sorted Array

You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.

Return _the single element that appears only once_.

Your solution must run in `O(log n)` time and `O(1)` space.

**Example 1:**

**Input:** nums = \[1,1,2,3,3,4,4,8,8\]
**Output:** 2

**Example 2:**

**Input:** nums = \[3,3,7,7,10,11,11\]
**Output:** 10

**Constraints:**

*   `1 <= nums.length <= 105`
*   `0 <= nums[i] <= 105`
Reverse String II

Given a string `s` and an integer `k`, reverse the first `k` characters for every `2k` characters counting from the start of the string.

If there are fewer than `k` characters left, reverse all of them. If there are less than `2k` but greater than or equal to `k` characters, then reverse the first `k` characters and leave the other as original.

**Example 1:**

**Input:** s = "abcdefg", k = 2
**Output:** "bacdfeg"

**Example 2:**

**Input:** s = "abcd", k = 2
**Output:** "bacd"

**Constraints:**

*   `1 <= s.length <= 104`
*   `s` consists of only lowercase English letters.
*   `1 <= k <= 104`
01 Matrix

Given an `m x n` binary matrix `mat`, return _the distance of the nearest_ `0` _for each cell_.

The distance between two adjacent cells is `1`.

**Example 1:**

**Input:** mat = \[\[0,0,0\],\[0,1,0\],\[0,0,0\]\]
**Output:** \[\[0,0,0\],\[0,1,0\],\[0,0,0\]\]

**Example 2:**

**Input:** mat = \[\[0,0,0\],\[0,1,0\],\[1,1,1\]\]
**Output:** \[\[0,0,0\],\[0,1,0\],\[1,2,1\]\]

**Constraints:**

*   `m == mat.length`
*   `n == mat[i].length`
*   `1 <= m, n <= 104`
*   `1 <= m * n <= 104`
*   `mat[i][j]` is either `0` or `1`.
*   There is at least one `0` in `mat`.
Diameter of Binary Tree

Given the `root` of a binary tree, return _the length of the **diameter** of the tree_.

The **diameter** of a binary tree is the **length** of the longest path between any two nodes in a tree. This path may or may not pass through the `root`.

The **length** of a path between two nodes is represented by the number of edges between them.

**Example 1:**

**Input:** root = \[1,2,3,4,5\]
**Output:** 3
**Explanation:** 3 is the length of the path \[4,2,1,3\] or \[5,2,1,3\].

**Example 2:**

**Input:** root = \[1,2\]
**Output:** 1

**Constraints:**

*   The number of nodes in the tree is in the range `[1, 104]`.
*   `-100 <= Node.val <= 100`
Output Contest Matches

During the NBA playoffs, we always set the rather strong team to play with the rather weak team, like make the rank `1` team play with the rank `nth` team, which is a good strategy to make the contest more interesting.

Given `n` teams, return _their final contest matches in the form of a string_.

The `n` teams are labeled from `1` to `n`, which represents their initial rank (i.e., Rank `1` is the strongest team and Rank `n` is the weakest team).

We will use parentheses `'('`, and `')'` and commas `','` to represent the contest team pairing. We use the parentheses for pairing and the commas for partition. During the pairing process in each round, you always need to follow the strategy of making the rather strong one pair with the rather weak one.

**Example 1:**

**Input:** n = 4
**Output:**  "((1,4),(2,3)) "
**Explanation:**
In the first round, we pair the team 1 and 4, the teams 2 and 3 together, as we need to make the strong team and weak team together.
And we got (1, 4),(2, 3).
In the second round, the winners of (1, 4) and (2, 3) need to play again to generate the final winner, so you need to add the paratheses outside them.
And we got the final answer ((1,4),(2,3)).

**Example 2:**

**Input:** n = 8
**Output:**  "(((1,8),(4,5)),((2,7),(3,6))) "
**Explanation:**
First round: (1, 8),(2, 7),(3, 6),(4, 5)
Second round: ((1, 8),(4, 5)),((2, 7),(3, 6))
Third round: (((1, 8),(4, 5)),((2, 7),(3, 6)))
Since the third round will generate the final winner, you need to output the answer (((1,8),(4,5)),((2,7),(3,6))).

**Constraints:**

*   `n == 2x` where `x` in in the range `[1, 12]`.
Boundary of Binary Tree

The **boundary** of a binary tree is the concatenation of the **root**, the **left boundary**, the **leaves** ordered from left-to-right, and the **reverse order** of the **right boundary**.

The **left boundary** is the set of nodes defined by the following:

*   The root node's left child is in the left boundary. If the root does not have a left child, then the left boundary is **empty**.
*   If a node in the left boundary and has a left child, then the left child is in the left boundary.
*   If a node is in the left boundary, has **no** left child, but has a right child, then the right child is in the left boundary.
*   The leftmost leaf is **not** in the left boundary.

The **right boundary** is similar to the **left boundary**, except it is the right side of the root's right subtree. Again, the leaf is **not** part of the **right boundary**, and the **right boundary** is empty if the root does not have a right child.

The **leaves** are nodes that do not have any children. For this problem, the root is **not** a leaf.

Given the `root` of a binary tree, return _the values of its **boundary**_.

**Example 1:**

**Input:** root = \[1,null,2,3,4\]
**Output:** \[1,3,4,2\]
**Explanation:**
- The left boundary is empty because the root does not have a left child.
- The right boundary follows the path starting from the root's right child 2 -> 4.
  4 is a leaf, so the right boundary is \[2\].
- The leaves from left to right are \[3,4\].
Concatenating everything results in \[1\] + \[\] + \[3,4\] + \[2\] = \[1,3,4,2\].

**Example 2:**

**Input:** root = \[1,2,3,4,5,6,null,null,null,7,8,9,10\]
**Output:** \[1,2,4,7,8,9,10,6,3\]
**Explanation:**
- The left boundary follows the path starting from the root's left child 2 -> 4.
  4 is a leaf, so the left boundary is \[2\].
- The right boundary follows the path starting from the root's right child 3 -> 6 -> 10.
  10 is a leaf, so the right boundary is \[3,6\], and in reverse order is \[6,3\].
- The leaves from left to right are \[4,7,8,9,10\].
Concatenating everything results in \[1\] + \[2\] + \[4,7,8,9,10\] + \[6,3\] = \[1,2,4,7,8,9,10,6,3\].

**Constraints:**

*   The number of nodes in the tree is in the range `[1, 104]`.
*   `-1000 <= Node.val <= 1000`
Remove Boxes

You are given several `boxes` with different colors represented by different positive numbers.

You may experience several rounds to remove boxes until there is no box left. Each time you can choose some continuous boxes with the same color (i.e., composed of `k` boxes, `k >= 1`), remove them and get `k * k` points.

Return _the maximum points you can get_.

**Example 1:**

**Input:** boxes = \[1,3,2,2,2,3,4,3,1\]
**Output:** 23
**Explanation:**
\[1, 3, 2, 2, 2, 3, 4, 3, 1\] 
----> \[1, 3, 3, 4, 3, 1\] (3\*3=9 points) 
----> \[1, 3, 3, 3, 1\] (1\*1=1 points) 
----> \[1, 1\] (3\*3=9 points) 
----> \[\] (2\*2=4 points)

**Example 2:**

**Input:** boxes = \[1,1,1\]
**Output:** 9

**Example 3:**

**Input:** boxes = \[1\]
**Output:** 1

**Constraints:**

*   `1 <= boxes.length <= 100`
*   `1 <= boxes[i] <= 100`