find 2 maximum of each level
This Question is Pretty much simple, \nJust read the explanation once,\n\nFirst of All you need to find the level, and height of each node of a tree and store t
This Question is Pretty much simple, \nJust read the explanation once,\n\nFirst of All you need to find the level, and height of each node of a tree and store them in a map\nThen store the 2 maximum node of each level\n\nNow start traversing in the queries array, suppose \nthe node you wants to remove is not having the maximum height in its level, \nthen the removal of that node, doesn\'t impact in height of tree,\n\nthe node you wants to remove is  having the maximum height in its level, \nthen the removal of that node, impact in height of tree,\nit decrease the height by\n(height of that node - 2nd maximum height of that level)\n\nso in that case queries[i]=heightOf(queries[i])-2nd maximum height of that level;\n\n**Happy coding :)**\n\n```\nclass Solution {\n    \n    // for storing the height of each node\n    Map<Integer,Integer> height=new HashMap<>();\n    // for storing the level of each node \n    Map<Integer,Integer> level=new HashMap<>();\n    // for storing the 2 maximum height of each level \n    Map<Integer,Queue<Integer>> twoMaximum=new HashMap<>();\n    \n    int nodeHeight(TreeNode root,int l){\n        \n        if(root==null) return -1;\n        // adding default priority Queue \n        twoMaximum.putIfAbsent(l,new PriorityQueue<>());\n        \n        int temp=Math.max(nodeHeight(root.left,l+1),nodeHeight(root.right,l+1))+1;\n        \n        height.put(root.val,temp);\n        level.put(root.val,l);\n        \n        twoMaximum.get(l).add(temp);\n        \n        // updating priority Queue with the 2 maximum height of current level \n        if(twoMaximum.get(l).size()>2) twoMaximum.get(l).remove();\n        return temp;\n    }\n    public int[] treeQueries(TreeNode root, int[] q) {\n        \n        // the height of tree \n        int h=nodeHeight(root,0);\n        \n        for(int i=0;i<q.length;i++){\n            int lev=level.get(q[i]);\n            int hh=height.get(q[i]);     \n            int a=-1,b=-1;\n            // if there is more then 2 node at the level of query[i]\n            if(twoMaximum.get(lev).size()==2){\n                b=twoMaximum.get(lev).remove();\n                a=twoMaximum.get(lev).remove();\n                \n                // if current node has maximum height, in his level then the height of tree will be impacted\n                // and height of tree will be decrease by heightOf(queries[i])-2nd maximum height of that level;\n                if(hh==a) q[i]=h-(a-b);\n                \n                // else the height of tree remains same \n                else q[i]=h;\n                \n                twoMaximum.get(lev).add(a);\n                twoMaximum.get(lev).add(b);\n            }\n            // if there is only one node at that level \n            // then the height of tree will be subtracted by the height of current node\n            else q[i]=h-hh-1;\n        }\n        // returning the updated query array \n        return q;\n    }\n}\n```\n\n**Time complexity will be O(N) \nSpace complexity will be O(N) + O(h) =O(N)**\n\n**where ,\nN is the number of nodes in a tree \nh is the height of tree**\n\n*Please Upvote the post.*
[]