LeetCode: Most Frequent Subtree Sum My solution to LeetCode problem Most Frequent Subtree Sum works, but I have several questions regarding the code. I am also looking for an advice on how to improve the code.


  Problem:
  
  Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). So what is the most frequent subtree sum value? If there is a tie, return all the values with the highest frequency in any order.
  
  Example 1

  5
 /  \
2   -3

  
  return [2, -3, 4], since all the values happen only once, return all of them in any order
  
  Example 2

  5
 /  \
2   -5

  
  return [2], since 2 happens twice, however -5 only occur once


My questions:


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? 
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?


My solution:

vector&lt;int&gt; find_frequent_tree_sum(TreeNode* root) {

    // Collect freqs of sums into map
    unordered_map&lt;int, int&gt; sum_freqs;
    get_sums_freqs (root, sum_freqs);

    // Get the max freq
    int max_freq = 0;
    unordered_map&lt;int, int&gt;::iterator it;
    for (it = sum_freqs.begin(); it != sum_freqs.end(); ++it) {
        max_freq = max(max_freq, it-&gt;second);
    }

    // Collect all the values, which freq == max_freq
    vector&lt;int&gt; res;
    //unordered_map&lt;int, int&gt;::iterator it;
    for (it = sum_freqs.begin(); it != sum_freqs.end(); ++it) {
        if (it-&gt;second == max_freq) {
            res.push_back(it-&gt;first);
        }
    }

    return res;
}

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

    if (root) {

        int curr_sum = root-&gt;val +
            get_sums_freqs (root-&gt;left, sum_freqs) + 
            get_sums_freqs (root-&gt;right, sum_freqs);

        sum_freqs[curr_sum]++;

        return curr_sum;

    } else {

        return 0;
    }
}


For context, here's the definition of TreeNode given by LeetCode:

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
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.