Skip to content

Commit

Permalink
added JS Most Frequent Subtree Sum
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezaghey committed Oct 25, 2019
1 parent 6c3e716 commit 0788a13
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions js/most-frequent-subtree-sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// https://leetcode.com/problems/most-frequent-subtree-sum/
// Related Topics: Hash Table, Tree, DFS
// Difficulty: Medium

/*
Initial thoughts:
Traversing the tree in a post order DFS manner, we are going to create
a freqency table of all the subtree sums and return those sums that occur
most at the end.
Time Complexity: O(n) where n == number of nodes in the tree
Space Complexity: O(n) where n == number of nodes in the tree (In a worst case
situation, each and every subtree has a unique sum that requires a separate entry
in our freqency table)
*/

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
const findFrequentTreeSum = root => {
if (!root) return [];

const m = new Map();

function dfs(root) {
if (!root) return 0;

const left = dfs(root.left);
const right = dfs(root.right);

const val = root.val + left + right;
if (m.has(val)) m.set(val, m.get(val) + 1);
else m.set(val, 1);
return val;
}

dfs(root);
let max = Number.NEGATIVE_INFINITY;
let finalRes;
for (let [key, val] of m.entries()) {
if (val > max) {
max = val;
finalRes = [key];
} else if (val === max) finalRes.push(key);
}
return finalRes;
};

0 comments on commit 0788a13

Please sign in to comment.