This two-part question evaluates skills in tree traversal and visible-node extraction for the binary-tree right-side view task and in array cumulative-sum reasoning for counting contiguous subarrays with a target sum, assessing data-structure manipulation and algorithmic problem-solving competency.

You are given two separate programming tasks.
You are given the root of a binary tree. Imagine standing on the right side of the tree and looking at it. You can see exactly one node at each depth level: the rightmost node at that level.
Write a function that returns a list (or array) of the values of the nodes that would be visible from the right side, ordered from top to bottom.
Consider this binary tree:
10
/ \
5 20
/ \ \
3 7 30
From the right side, you see nodes with values: [10, 20, 30].
root
node of a binary tree where each node has:
val
: integer
left
: left child pointer (or null)
right
: right child pointer (or null)
n
can be up to about
10^4
.
You are given an integer array nums and an integer k.
Return the total number of contiguous subarrays whose elements sum exactly to k.
nums = [1, 2, 3]
,
k = 3
[1, 2]
(sum = 3)
[3]
(sum = 3)
So the answer is
2
.
nums
of length
n
.
k
.
k
.
1 <= n <= 10^5
(you should aim for an algorithm that is close to O(n)).
nums
can be negative, zero, or positive (32-bit signed integers).
k
is a 32-bit signed integer.
You may implement both functions in any language of your choice.