Skip to content

Commit 12731da

Browse files
committed
package com.github.masx200.leetcode_test.er_cha_sou_suo_shu_de_di_kda_jie_dian_lcof
1 parent 4b4877f commit 12731da

File tree

3 files changed

+41
-0
lines changed
  • er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof
  • test/com/github/masx200/leetcode_test/er_cha_sou_suo_shu_de_di_kda_jie_dian_lcof

3 files changed

+41
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.github.masx200.leetcode_test.er_cha_sou_suo_shu_de_di_kda_jie_dian_lcof
2+
3+
import com.github.masx200.leetcode_test.insert_into_a_binary_search_tree.TreeNode
4+
5+
class Solution {
6+
fun kthLargest(root: TreeNode?, k: Int): Int {
7+
8+
reverseInOrderIterator(root).forEachIndexed { i, v ->
9+
if (i + 1 == k) return v
10+
11+
}
12+
throw Error("unreachable")
13+
}
14+
15+
}
16+
17+
fun reverseInOrderIterator(root: TreeNode?): Sequence<Int> {
18+
return sequence {
19+
20+
if (root == null) return@sequence
21+
22+
yieldAll(reverseInOrderIterator(root.right))
23+
yield(root.`val`)
24+
yieldAll(reverseInOrderIterator(root.left))
25+
}
26+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<sourceDirs>
8989
<sourceDir>construct-binary-search-tree-from-preorder-traversal</sourceDir>
9090
<source>er-cha-shu-ran-se-UGC</source>
91+
<source>er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof</source>
9192
<source>utils</source>
9293
<source>add-two-integers</source>
9394
<source>design-skiplist</source>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.github.masx200.leetcode_test.er_cha_sou_suo_shu_de_di_kda_jie_dian_lcof
2+
3+
import com.github.masx200.leetcode_test.utils.TreeNodeLeetCodeParse
4+
import org.junit.jupiter.api.Assertions.assertEquals
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
9+
@Test
10+
fun testkthLargest() {
11+
assertEquals(4, Solution().kthLargest(TreeNodeLeetCodeParse("[5,3,6,2,4,null,null,1]"), 3))
12+
assertEquals(4, Solution().kthLargest(TreeNodeLeetCodeParse(" [3,1,4,null,2]"), 1))
13+
}
14+
}

0 commit comments

Comments
 (0)