Just write this verbose solution without any optimization. Its good for understanding the basic idea of two pass, but it bad for a good solution should be.
The second pass can be optimized by keeping track of the max count of the tree node value through the first pass.