Just a small optimization instead of doing v[root->val]++ and later v[root->val]-- because you called by reference , you can just call the function by value it won't make a difference much in run time but just wanted to say that would make it a bit easier to understand