JAVA | Very EASY | DFS | Memoization | 100% Faster
\tint[] h, l, r;\n\t// Store the height of each node\'s left and right child\n    public int height(TreeNode root){\n        if(root == null) return 0;\n       
\tint[] h, l, r;\n\t// Store the height of each node\'s left and right child\n    public int height(TreeNode root){\n        if(root == null) return 0;\n        \n        int lh = height(root.left);\n        int rh = height(root.right);\n        \n        l[root.val] = lh;\n        r[root.val] = rh;\n        \n        return 1+Math.max(lh, rh);\n    }\n\t// Store the max height of each node if deleted\n    public void solve(TreeNode root, int curmax, int depth){\n        if(root == null) return;\n        \n        h[root.val] = curmax;\n\t\t//max of (current depth + right/left height, maxSoFar)\n        solve(root.left, Math.max(curmax, depth+r[root.val]), depth+1);\n        solve(root.right, Math.max(curmax, depth+l[root.val]), depth+1);\n    }\n    public int[] treeQueries(TreeNode root, int[] queries) {\n        h = new int[100001];\n        l = new int[100001];\n        r = new int[100001];\n        \n        height(root);\n        solve(root.left, r[root.val], 1);\n        solve(root.right, l[root.val], 1);\n        \n        int n = queries.length;\n        int[] q = new int[n];\n        for(int i=0; i<n; i++){\n            q[i] = h[queries[i]]; //Query in O(1)\n        }\n        return q;\n    }
['Depth-First Search', 'Breadth-First Search', 'Java']