7 lines scala dfs recursion
scala []\n/**\n * Definition for a binary tree node.\n * class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {\n *   var value: Int
```scala []\n/**\n * Definition for a binary tree node.\n * class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {\n *   var value: Int = _value\n *   var left: TreeNode = _left\n *   var right: TreeNode = _right\n * }\n */\n\nobject Solution {\n  def kthLargestPerfectSubtree(root: TreeNode, k: Int): Int =\n    def f(node: TreeNode): (Option[Int],List[Int]) =\n      lazy val cs = List(Option(node.left),Option(node.right)).flatten.map(f)\n      lazy val cs1 = cs.flatMap(_._1)\n      lazy val cnt  = Option.when(cs1.length==2 && cs1.head == cs1.last)(cs1.sum + 1)\n      lazy val lst  = cnt.toList ++ cs.flatMap(_._2).sortBy(- _).take(k)\n      if(node.left == null && node.right == null) (Some(1),List(1)) else (cnt,lst)\n    f(root)._2.drop(k-1).headOption.getOrElse(-1)\n}\n```
['Depth-First Search', 'Recursion', 'Sorting', 'Binary Tree', 'Scala']