Easy solution beats 100%
\n\n# Code\njavascript []\n\nvar treeQueries = function(root, queries) {\n    const heights = new Array(50000).fill(0);\n    const d = new Array(100001).fill(0)
\n\n# Code\n```javascript []\n\nvar treeQueries = function(root, queries) {\n    const heights = new Array(50000).fill(0);\n    const d = new Array(100001).fill(0);\n    const l = new Array(100001).fill(0);\n    const r = new Array(100001).fill(0);\n    let len = 0;\n\n    function search(p, h) {\n        d[p.val] = h;\n\n        if (!p.left && !p.right) {\n            heights[len] = h;\n            l[p.val] = r[p.val] = len;\n            len++;\n            return;\n        }\n\n        l[p.val] = len;\n\n        if (p.left) search(p.left, h + 1);\n        if (p.right) search(p.right, h + 1);\n\n        r[p.val] = len - 1;\n    }\n\n    search(root, 0);\n\n    const n = len;\n    const maxl = new Array(n).fill(0);\n    const maxr = new Array(n).fill(0);\n\n    for (let i = 1; i < n; i++) {\n        maxl[i] = Math.max(maxl[i-1], heights[i-1]);\n        maxr[n-i-1] = Math.max(maxr[n-i], heights[n-i]);\n    }\n\n    const ret = [];\n    const k = queries.length;\n\n    for (let i = 0; i < k; i++) {\n        const maxxl = maxl[l[queries[i]]];\n        const maxxr = maxr[r[queries[i]]];\n        ret.push(Math.max(Math.max(maxxl, maxxr), d[queries[i]] - 1));\n    }\n\n    return ret;\n};\n```
['JavaScript']