less code beats 100%| single traversal | visualization with added YouTube video
YouTube explanation :- https://youtu.be/IpES7PqU5QU\n# Intuition and approach\nlets start with certain observations :-\n \n1) at any node in binary tree the dep
# YouTube explanation :- https://youtu.be/IpES7PqU5QU\n# Intuition and approach\nlets start with certain observations :-\n \n**1)** at any node in binary tree the **depth** of the node at given **level** of the node = **1 + max(height(left child, level+1), height(right child, level+1))**\n\n**2)** what if i keep an array that can save the **maximum depth** and **second maximum depth** at every level and also the level of every node at **levelArr[]**, we can keep easily with help of an array.\n\n**now some you might have question, why 2nd maximum as well??**\n\n**3)** the intuition is whenever we are at a query for a node value = val ===>  levelArr[val] = level\n          depth[val] = 1 + Math.max(height(root.left, level+1), height(root.right, level+1))\n\nnow after removing any node in a given level there are two possiblity\n\n**height of tree = level + max depth of any node on that level -1**\n\nnow  **1)**what if the node that make biggest depth is the node itself the one we gonna remove??\n\n==> so in that case we need to pick the second maximum depth.\n\n**2)** otherwise if this is not the biggest height node in that level than we need maximum depth.\n\nso now it should be clear we need to keep trak of**max and 2nd max** in arrays for depth at each level.\n\nso at every query we have return **maxDepth at level** + level -1, accordingly as per case **1** or **2** above.\n\n**still have doubt?? no problem will release YouTube soon**\n\n\n\n\n# Code\n```java []\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 depth[];\n    int levelArr[];\n    int max1[];\n    int max2[];\n    public int height(TreeNode root, int level)\n    {\n     if(root==null)\n         return 0; \n     levelArr[root.val] = level; \n     depth[root.val] = 1 + Math.max(height(root.left, level+1), height(root.right, level+1));\n     if(max1[level]<depth[root.val]) \n     {\n         max2[level] = max1[level];\n         max1[level] = depth[root.val];\n     }\n        else if(max2[level]<depth[root.val])\n            max2[level] = depth[root.val]; \n     return depth[root.val];\n    }\n    \n    \n    public int[] treeQueries(TreeNode root, int[] queries) {\n        depth = new int[100001];\n        levelArr = new int[100001];\n        max1 = new int[100001];\n        max2 = new int[100001];\n        \n        height(root, 0);\n        for(int i=0; i<queries.length; i++)\n        {\n         int q = queries[i];\n         int level = levelArr[q];\n         queries[i] = (max1[level]==depth[q]? max2[level] : max1[level]) + level - 1;  \n            \n        }\n        \n       return queries; \n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    vector<int> depth, levelArr, max1, max2;\n\n    int height(TreeNode* root, int level) {\n        if (!root) return 0;\n        levelArr[root->val] = level;\n        depth[root->val] = 1 + max(height(root->left, level + 1), height(root->right, level + 1));\n\n    \n        if (max1[level] < depth[root->val]) {\n            max2[level] = max1[level];\n            max1[level] = depth[root->val];\n        } else if (max2[level] < depth[root->val]) {\n            max2[level] = depth[root->val];\n        }\n\n        return depth[root->val];\n    }\n\n    vector<int> treeQueries(TreeNode* root, vector<int>& queries) {\n        depth.resize(100001, 0);\n        levelArr.resize(100001, 0);\n        max1.resize(100001, 0);\n        max2.resize(100001, 0);\n\n        // Compute depths and max depths for each level\n        height(root, 0);\n\n        // Process each query\n        for (int i = 0; i < queries.size(); i++) {\n            int q = queries[i];\n            int level = levelArr[q];\n            queries[i] = (max1[level] == depth[q] ? max2[level] : max1[level]) + level - 1;\n        }\n\n        return queries;\n    }\n};\n\n```\n```python []\n# Definition for a binary tree node.\n# class TreeNode(object):\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\n\nclass Solution(object):\n    def __init__(self):\n        self.depth = [0] * 100001\n        self.levelArr = [0] * 100001\n        self.max1 = [0] * 100001\n        self.max2 = [0] * 100001\n\n    def height(self, root, level):\n        if not root:\n            return 0\n        \n        # Set the level and calculate depth for the current node\n        self.levelArr[root.val] = level\n        self.depth[root.val] = 1 + max(self.height(root.left, level + 1), self.height(root.right, level + 1))\n        \n        # Update the highest and second-highest depths for the current level\n        if self.max1[level] < self.depth[root.val]:\n            self.max2[level] = self.max1[level]\n            self.max1[level] = self.depth[root.val]\n        elif self.max2[level] < self.depth[root.val]:\n            self.max2[level] = self.depth[root.val]\n        \n        return self.depth[root.val]\n\n    def treeQueries(self, root, queries):\n        """\n        :type root: Optional[TreeNode]\n        :type queries: List[int]\n        :rtype: List[int]\n        """\n        # Compute depths and max depths for each level\n        self.height(root, 0)\n\n        # Process each query\n        result = []\n        for q in queries:\n            level = self.levelArr[q]\n            \n            # Calculate the result for each query based on max depths at the level\n            result.append((self.max2[level] if self.max1[level] == self.depth[q] else self.max1[level]) + level - 1)\n        \n        return result\n\n```\n```C# []\npublic class Solution {\n    private int[] depth = new int[100001];\n    private int[] levelArr = new int[100001];\n    private int[] max1 = new int[100001];\n    private int[] max2 = new int[100001];\n\n    private int Height(TreeNode root, int level) {\n        if (root == null) return 0;\n\n        // Set the level and calculate depth for the current node\n        levelArr[root.val] = level;\n        depth[root.val] = 1 + Math.Max(Height(root.left, level + 1), Height(root.right, level + 1));\n\n        // Update the highest and second-highest depths for the current level\n        if (max1[level] < depth[root.val]) {\n            max2[level] = max1[level];\n            max1[level] = depth[root.val];\n        } else if (max2[level] < depth[root.val]) {\n            max2[level] = depth[root.val];\n        }\n\n        return depth[root.val];\n    }\n\n    public int[] TreeQueries(TreeNode root, int[] queries) {\n        // Compute depths and max depths for each level\n        Height(root, 0);\n\n        // Process each query\n        for (int i = 0; i < queries.Length; i++) {\n            int q = queries[i];\n            int level = levelArr[q];\n\n            // Set result for each query based on max depths at the level\n            queries[i] = (max1[level] == depth[q] ? max2[level] : max1[level]) + level - 1;\n        }\n\n        return queries;\n    }\n}\n\n```\n\n\n\n
['Array', 'Math', 'Tree', 'Memoization', 'Binary Tree', 'Python', 'C++', 'Java', 'C#']