LeetCode: Most Frequent Subtree Sum
The function get_sums_freqs returns an int, but when I call it, I do not assign the result to anything: get_sums_freqs (root, sum_freqs);. Is it ok to do it this way? 


Your uneasiness here probably arises from the difference between a Tree and a TreeNode.  A TreeNode is an internal tree detail, which should be hidden from the user of the Tree.  I should be able to pass an Tree object to some function, and have it add some number of nodes to the Tree.  If I pass an nullptr as the TreeNode * root, the caller can't modify my tree.  Similarly, if two entities are supposed to share access to a Tree, that is fine as long as the TreeNode *root contains at least one node.  But you can't share a common empty Tree; instead you have two nullptr ... which are not "shareable".

class Tree {
    TreeNode *root;
    int num_nodes;
    int max_depth;
}


Now we have a Tree object, which can be shared ... even if it is an empty tree.  As a bonus, our Tree can hold meta-information about the tree, such as the number of nodes in the tree, the maximum depth of the tree, and so on.

The LeetCode challenge is promoting a broken tree model, by using a TreeNode * to represent a tree.  It works, but it has issues; I wouldn't want to see it used in a professional "Project".

I prefer not to walk into nullptr nodes.  Imagine:

void TreeNode::get_sums_freqs(unordered_map&lt;int, int&gt;&amp; sum_freqs) { ... }


Now, you would be calling TreeNode::get_sums_freqs() with a nullptr for this, which is bad.

Instead:

int get_sums_freqs(TreeNode* node, unordered_map&lt;int, int&gt;&amp; sum_freqs) {

    int curr_sum = root-&gt;val;

    if (node-&gt;left)
       curr_sum += get_sums_freqs(node-&gt;left, sum_freqs);
    if (node-&gt;right)
       curr_sum += get_sums_freqs(node-&gt;right, sum_freqs);

    sum_freqs[curr_sum]++;

    return curr_sum;
}


Note we are calling the current node node, instead of root.  We need to start off the search at the root of the tree, which is a little different:

void tree_get_sums_freqs(Tree* tree, unordered_map&lt;int, int&gt; &amp;sum_freqs) {
    if (tree-&gt;root) {
       root_sum = get_sums_freqs(tree-&gt;root, sum_freqs);
       sum_freqs[root_sum]++;
    }
}


No root_sum needs to be returned from this top-level.

Of course, since LeetCode doesn't define their trees this way, you have your odd, unused return value.


  For some reason, it feels like a bad style to me to initialize the hash map unordered_map&lt;int, int&gt; sum_freqs; in one function and then pass it to another function to fill it out. Is there a better way to do it? Or is this a correct way?


This is fine.  It is a visitor pattern.  You create an accumulator object, and visit every node of your structure with it, to accumulate the results.