You are given the root of an N-ary tree (each node can have 0..k children). Implement the Node class yourself.
Assume each node stores an integer value and a list of child nodes children.
Complete the following tasks:
-
Sum of all nodes
: Return the sum of
value
over
all
nodes in the tree.
-
Maximum path value
: Define a
path
as a sequence of nodes from the
root to any leaf
(a node with no children). The
path value
is the sum of node values along that path. Return the
maximum
root-to-leaf path value.
-
Return the max path
: Return the list of node values along one root-to-leaf path that achieves the maximum path value from (2). If multiple paths tie, you may return any one of them.
Input / Output
-
Input:
root: Node | null
-
Output:
-
(1) an integer total sum
-
(2) an integer maximum root-to-leaf path sum
-
(3) an array/list of integers representing the values along the chosen max path
Notes / Constraints
-
The tree can be empty (
root = null
).
-
Node values can be negative, zero, or positive.
-
Aim for linear time in the number of nodes.