You are given four independent coding tasks. For each task, implement the required function.
Problem 1: Zigzag level-order traversal of a binary tree
Given the root of a binary tree, return the node values level by level, but alternate the traversal direction each level:
-
Level 0 (root level): left → right
-
Level 1: right → left
-
Level 2: left → right
-
… and so on.
Input: root (binary tree node)
Output: A list of lists, where each inner list contains the values for one level in the required order.
Example:
-
Tree:
1,2,3,4,5,null,6
-
Output:
[[1],[3,2],[4,5,6]]
Constraints: number of nodes up to ~10^4.
Problem 2: Longest palindromic subsequence length
Given a string s, return the length of the longest subsequence of s that is a palindrome.
-
A
subsequence
can delete characters without changing the relative order of the remaining characters.
Input: string s
Output: integer length
Example:
-
Input:
s = "bbbab"
-
Output:
4
(one answer is subsequence
"bbbb"
)
Constraints: 1 <= len(s) <= 1000.
Problem 3: Integer square root (floor)
Given a non-negative integer x, compute and return ⌊sqrt(x)⌋ (the integer part of the square root).
Input: integer x >= 0
Output: integer r such that r*r <= x < (r+1)*(r+1)
Example:
Constraints: 0 <= x <= 2^31 - 1.
Problem 4: Spreadsheet column label to number
In a spreadsheet, columns are labeled A, B, ..., Z, AA, AB, ..., AZ, BA, ....
Given a column label string col consisting of uppercase English letters, return its 1-indexed column number.
Input: string col
Output: integer
Examples:
-
"A" -> 1
-
"Z" -> 26
-
"AA" -> 27
-
"ZY" -> 701
Constraints: 1 <= len(col) <= 7 (or any length that fits within 32-bit/64-bit integer, depending on language).