Facebook | phone | Average sum of descendants equal to node value
Anonymous User
536

Similar question like this one https://leetcode.com/problems/maximum-average-subtree/

I don't remeber 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) 
Comments (2)