Easiest Python Solution 🔥✅ | DFS
Intuition\nTo solve this problem, we want to find all the perfect binary subtrees in a given binary tree and determine their sizes. A perfect binary tree is one
# Intuition\nTo solve this problem, we want to find all the perfect binary subtrees in a given binary tree and determine their sizes. A perfect binary tree is one where every level is fully filled, meaning all the leaves are at the same level, and every parent has two children, and their children are perfect.\n\n# Approach\n1. **Recursive Traversal**: We will use a recursive function to traverse the binary tree. For each node, we check if it can be the root of a perfect binary subtree.\n2. **Check Children**:\n    - If both left and right children are perfect subtrees of the same size, then the current node is also part of a perfect subtree.\n    - If both left and right children are absent (the node is a leaf), it\u2019s also a perfect subtree of size 1.\n    - If the sizes of the left and right subtrees are not the same or one of them is invalid, we cannot consider the current node as the root of a perfect subtree.\n3. **Store Sizes**: We maintain a list to store the sizes of all found perfect binary subtrees.\n4. **Sorting and Returning the Result**: After collecting all sizes, we sort them in descending order and return the K-th largest size. If we don\u2019t have enough sizes, we return -1.\n\n# Complexity\n- Time complexity: $$O(n)$$, where n is the number of nodes in the tree.\n\n- Space complexity: $$O(n)$$\n\n# Code\n```python3 []\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution:\n    def kthLargestPerfectSubtree(self, root: Optional[TreeNode], k: int) -> int:\n        ans = []\n        def rec(root):\n            if not root: return 0\n            l = rec(root.left)\n            r = rec(root.right)\n            if l != r or l == -1 or r == -1: return -1\n            if (l == r and l != 0) or (l == 0 and not root.left and not root.right):\n                ans.append(l + r + 1)\n            return l + r + 1\n        rec(root)\n        ans.sort(reverse = True)\n        return ans[k - 1] if len(ans) >= k else -1\n```
['Depth-First Search', 'Recursion', 'Binary Tree', 'Python', 'Python3']