File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed
Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ package leet_code
2+
3+ import (
4+ "testing"
5+ )
6+
7+ //二叉搜索树的第k大节点
8+ func kthLargest (root * TreeNode , k int ) int {
9+ if root == nil {
10+ return 0
11+ }
12+ var data []int
13+ kthLargestV1 (root , & data )
14+ qSort (data )
15+ return data [k - 1 ]
16+ }
17+
18+ func qSort (num []int ) {
19+ if len (num ) <= 1 {
20+ return
21+ }
22+ var (
23+ start = 0
24+ end = len (num ) - 1
25+ mod = num [0 ]
26+ )
27+ for start < end {
28+ if mod < num [start + 1 ] {
29+ num [start ], num [start + 1 ] = num [start + 1 ], num [start ]
30+ start ++
31+ } else {
32+ num [start + 1 ], num [end ] = num [end ], num [start + 1 ]
33+ end --
34+ }
35+ }
36+ qSort (num [:start ])
37+ qSort (num [start + 1 :])
38+ }
39+
40+ func kthLargestV1 (root * TreeNode , d * []int ) {
41+ if root == nil {
42+ return
43+ }
44+ * d = append (* d , root .Val )
45+ kthLargestV1 (root .Left , d )
46+ kthLargestV1 (root .Right , d )
47+ }
48+
49+ func Test_kthLargest (t * testing.T ) {
50+ }
You can’t perform that action at this time.
0 commit comments