Observation + Approach || Simple and Easy Implementation
Example:- \n    \n    K=2\n\n                    5                    --LEVEL 0\n                /       \\n              3           6              --LEVEL 1\n
Example:- \n    \n    K=2\n\n                    5                    --LEVEL 0\n                /       \\\n              3           6              --LEVEL 1\n            /   \\        /   \\\n          5      2      5     7          --LEVEL 2\n        /   \\          /  \\ \n       1     8        6     8            --LEVEL 4\n\n    ANS = 3   (size of 2nd largest perfect subtree)\n\n# Observation\n\n1. If current subtree is not perfect binary tree then all ancesstors of current subtree will not be perfect binary tree.\n    \n    - Example: \n            \n        At level 1 : Subtree at node 3 is not perfect binary tree therefore its parent node 5 cannot form perfect binary tree\n\n2. If curr Node have only 1 children it cannot form perfect binary subtree  (consider example 3 in description)\n\n3. If leftSubtree size is not equal to right subtree then currNode cannot create perfect binary tree.\n\n     - Example:\n     \n       At level 1 : Node 3\'s leftSubtree size (3) != rightSubtree size (1)\n\n           *** note: both left and right subtree of Node 3 are perfect binary subtree but Node 3 is not perfect binary subtree\n\n\n\n# Implementation:\n   Build the logic around above 3 observations , refer to code below for more clarity and give it a dry run.\n\n# Code\n```java []\nclass Solution {\n    PriorityQueue<Integer> pq = new PriorityQueue<>();\n    public int helper(TreeNode root,int k){\n        if(root==null){\n            return 0;\n        }\n        int left = helper(root.left,k);\n        int right = helper(root.right,k);\n        if((root.left!=null && root.right==null) || (root.right!=null && root.left==null)){\n            return -1;\n        }\n        if(left==-1 || right==-1 || left!=right){\n            return -1;\n        }\n        if(pq.size()<k){\n            pq.add(left+right+1);\n        }else{\n            if(pq.peek() < left+right+1){\n                pq.poll();\n                pq.add(left+right+1);\n            }\n        }\n        return left+right+1;\n    }\n    public int kthLargestPerfectSubtree(TreeNode root, int k) {\n        helper(root,k);\n        if(pq.size()<k){\n            return -1;\n        }\n        return pq.peek();\n    }\n}\n```\n# Thanks! Please upvote\n.\n.\n.\n\n
['Java']