Facebook | phone | Average sum of descendants equal to node value
Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2022-02-01Added By Anonymous
Source: Link
Company: Meta (Facebook)
Role: unknown
Interview Stage: Phone Screen
Location: unknown
Year: unknown
Original Post Content:
Similar question like this one https://leetcode.com/problems/maximum-average-subtree/I don't remember if you are supposed to count the root value when doing the avg but the question was something like this.
Traverse each node in the tree and get the avg from the subtree. The avg should then equal to the current node val. If all nodes equal to their subtree avg return true else false;
ex current node root is 2
and the subtree sum will be {2,2,2,2} = 8 and count = 4, the avg = 2 and that is equal to root value of 2.
// valid tree
// 2
// / \
// 2 2
// / \ /\
// 2 2
//// invalid tree
// 2
// / \
// 3 2
// / \ /\
// 2 2
//boolean isAvg(Node root)