##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
4 1 5 2 1 0 0 1
1
1 7
0
3 5 5 5 0 0
0
Given an N-ary tree with nodes rooted at node , where each node has an integer value, find the node whose subtree (including itself) has the maximum average value.
If there are ties, return the node with the smallest index.
The subtree of a node includes the node itself and all of its descendants.
To avoid floating-point issues, compare averages using cross-multiplication: compare against .
Subtree averages: Node 0: . Node 1: . Node 2: . Node 3: . Node 1 has the maximum average of 3.0. Answer: 1.
Only one node (node 0) with value . Its subtree average is . Answer: 0.
Subtree averages: Node 0: . Node 1: . Node 2: . All averages are equal. Tie-break by smallest index. Answer: 0.