Search code examples
javascriptarraystreeheap-memory

Why am I getting a heap error when I run this program for large inputs?


I am working on this problem https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/description/ and I have created a solution that works fine for all my small testcases but the program causes a heap allocation error when it is run with a large input.

FATAL ERROR: MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory

Here's my solution:

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var pseudoPalindromicPaths = function (root) {
    var total = 0
    var dfs = (node, parents) => {
        parents.push(node.val)
        if (!node.left && !node.right) {
            if (isPalindromic(parents)) {
                total++
            }
            return;
        }
        if (node.left) {
            dfs(node.left, [...parents])
        }
        if (node.right) {
            dfs(node.right, [...parents])
        }
    }
    dfs(root, [])
    return total;
};

const isPalindromic = (arr) => {
    let numberCounts = {
        "1": 0,
        "2": 0,
        "3": 0,
        "4": 0,
        "5": 0,
        "6": 0,
        "7": 0,
        "8": 0,
        "9": 0
    }
    for (var i = 0; i < arr.length; i++) {
        numberCounts[arr[i]]++
    }
    var res = Object.values(numberCounts)
    var oddCount = 0
    for (var i = 0; i < res.length; i++) {
        if (res[i] % 2 !== 0) {
            oddCount++
        }
        if (oddCount > 1) {
            return false
        }
    }
    return true
}

How can I make this more efficient so that it runs within the memory limit?


Solution

  • Ok, I figured it out.

    You cannot use an array to store the path history. You need to do the path counting inline rather than after I reach a leaf node.

    /**
     * Definition for a binary tree node.
     * function TreeNode(val, left, right) {
     *     this.val = (val===undefined ? 0 : val)
     *     this.left = (left===undefined ? null : left)
     *     this.right = (right===undefined ? null : right)
     * }
     */
    /**
     * @param {TreeNode} root
     * @return {number}
     */
    var pseudoPalindromicPaths = function (root) {
        var total = 0
        let parents = {
            "1": 0,
            "2": 0,
            "3": 0,
            "4": 0,
            "5": 0,
            "6": 0,
            "7": 0,
            "8": 0,
            "9": 0
        }
    
        var dfs = (node, parents) => {
            parents[node.val]++
            if (!node.left && !node.right) {
                if (isPalindromic(parents)) {
                    total++
                }
                return;
            }
            if (node.right) {
                dfs(node.right, {...parents})
            }
            if (node.left) {
                dfs(node.left, {...parents})
            }
        }
        dfs(root, parents)
        return total;
    };
    
    const isPalindromic = (obj) => {
        var res = Object.values(obj)
        var oddCount = 0
        for (var i = 0; i < res.length; i++) {
            if (res[i] % 2 !== 0) {
                oddCount++
            }
            if (oddCount > 1) {
                return false
            }
        }
        return true
    
    }