Beats 100% | 0 ms | 16 ms
Code\n\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode(
# Code\n```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\n *     TreeNode(int val) { this.val = val; }\n *     TreeNode(int val, TreeNode left, TreeNode right) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\nclass Solution {\n    int leftMax;\n    int rightMax;\n    // HashMap<Integer, Integer> allLevel;\n    int[] allL;\n    public int[] treeQueries(TreeNode root, int[] queries) {\n        leftMax = 0;\n        rightMax = 0;\n        // allLevel = new HashMap<>();\n        allL = new int[100001];\n        int[] res = new int[queries.length];\n        dfsLeft(root, 0);\n        dfsRight(root, 0);\n        for(int i = 0; i < queries.length; i++){\n            res[i] = allL[queries[i]];\n        }\n        return res;\n    }\n    \n    public void dfsLeft(TreeNode root, int level){\n        if(root == null) return;\n        allL[root.val] = leftMax;\n        leftMax = Math.max(leftMax, level);\n        dfsLeft(root.left, level + 1);\n        dfsLeft(root.right, level + 1);\n    }\n    \n     public void dfsRight(TreeNode root, int level){\n        if(root == null) return;\n        allL[root.val] = Math.max(rightMax, allL[root.val]);\n        rightMax = Math.max(rightMax, level);\n        dfsRight(root.right, level + 1);\n        dfsRight(root.left, level + 1);\n    }\n}\n```
['Java']