51
51
# self.right = None
52
52
53
53
class Solution :
54
- t, res = 0 , - 1
55
54
def kthLargest (self , root : TreeNode, k : int ) -> int :
56
- self .t = k
57
- self ._traverse(root)
58
- return self .res
59
-
60
- def _traverse (self , node ):
61
- if node:
62
- self ._traverse(node.right)
63
- self .t -= 1
64
- if self .t == 0 :
65
- self .res = node.val
55
+ def inorder (root ):
56
+ if root is None :
57
+ return
58
+ inorder(root.right)
59
+ self .cur -= 1
60
+ if self .cur == 0 :
61
+ self .res = root.val
66
62
return
67
- self ._traverse(node.left)
63
+ inorder(root.left)
64
+
65
+ self .cur = k
66
+ inorder(root)
67
+ return self .res
68
68
```
69
69
70
70
### ** Java**
@@ -80,24 +80,27 @@ class Solution:
80
80
* }
81
81
*/
82
82
class Solution {
83
- private int t ;
83
+ private int cur ;
84
84
private int res;
85
+
85
86
public int kthLargest (TreeNode root , int k ) {
86
- t = k;
87
- traverse(root);
87
+ cur = k;
88
+ res = 0 ;
89
+ inorder(root);
88
90
return res;
89
91
}
90
92
91
- private void traverse (TreeNode node ) {
92
- if (node ! = null ) {
93
- traverse(node . right) ;
94
- -- t;
95
- if (t == 0 ) {
96
- res = node . val ;
97
- return ;
98
- }
99
- traverse(node . left) ;
93
+ private void inorder (TreeNode root ) {
94
+ if (root = = null ) {
95
+ return ;
96
+ }
97
+ inorder(root . right);
98
+ -- cur ;
99
+ if (cur == 0 ) {
100
+ res = root . val;
101
+ return ;
100
102
}
103
+ inorder(root. left);
101
104
}
102
105
}
103
106
```
@@ -117,44 +120,60 @@ class Solution {
117
120
* @param {number} k
118
121
* @return {number}
119
122
*/
120
- var kthLargest = function (root , k ) {
121
- let res;
122
- let t = 0 ;
123
- function traversal (node ) {
124
- if (! node) return ;
125
- traversal (node .right );
126
- if (++ t === k) res = node .val ;
127
- traversal (node .left );
128
- }
129
- traversal (root);
130
- return res;
123
+ var kthLargest = function (root , k ) {
124
+ const inorder = (root ) => {
125
+ if (! root) {
126
+ return ;
127
+ }
128
+ inorder (root .right );
129
+ -- cur;
130
+ if (cur == 0 ) {
131
+ res = root .val ;
132
+ return ;
133
+ }
134
+ inorder (root .left );
135
+ }
136
+ let res = 0 ;
137
+ let cur = k;
138
+ inorder (root);
139
+ return res;
131
140
};
132
141
```
133
142
134
143
### ** C++**
135
144
136
145
``` cpp
146
+ /* *
147
+ * Definition for a binary tree node.
148
+ * struct TreeNode {
149
+ * int val;
150
+ * TreeNode *left;
151
+ * TreeNode *right;
152
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
153
+ * };
154
+ */
137
155
class Solution {
138
156
public:
139
157
int kthLargest(TreeNode* root, int k) {
140
158
cur = k;
141
- inOrder (root);
159
+ inorder (root);
142
160
return res;
143
161
}
144
162
145
163
private:
146
164
int cur, res;
147
165
148
- void inOrder(TreeNode* root) {
149
- if (root) {
150
- inOrder(root->right);
151
- --cur;
152
- if (cur == 0) {
153
- res = root->val;
154
- return;
155
- }
156
- inOrder (root->left);
166
+ void inorder(TreeNode* root) {
167
+ if (!root) {
168
+ return;
157
169
}
170
+ inorder (root->right);
171
+ --cur;
172
+ if (cur == 0) {
173
+ res = root->val;
174
+ return;
175
+ }
176
+ inorder(root->left);
158
177
}
159
178
};
160
179
```
@@ -164,26 +183,34 @@ private:
164
183
利用 Go 的特性,中序遍历“生产”的数字传到 `channel`,返回第 `k` 个。
165
184
166
185
```go
186
+ /**
187
+ * Definition for a binary tree node.
188
+ * type TreeNode struct {
189
+ * Val int
190
+ * Left *TreeNode
191
+ * Right *TreeNode
192
+ * }
193
+ */
167
194
func kthLargest(root *TreeNode, k int) int {
168
195
ch := make(chan int)
169
196
ctx, cancel := context.WithCancel(context.Background())
170
197
defer cancel()
171
- go inOrder (ctx, root, ch)
198
+ go inorder (ctx, root, ch)
172
199
for ; k > 1; k-- {
173
200
<-ch
174
201
}
175
202
return <-ch
176
203
}
177
204
178
- func inOrder (ctx context.Context, cur *TreeNode, ch chan<- int) {
205
+ func inorder (ctx context.Context, cur *TreeNode, ch chan<- int) {
179
206
if cur != nil {
180
- inOrder (ctx, cur.Right, ch)
207
+ inorder (ctx, cur.Right, ch)
181
208
select {
182
209
case ch <- cur.Val:
183
210
case <-ctx.Done():
184
211
return
185
212
}
186
- inOrder (ctx, cur.Left, ch)
213
+ inorder (ctx, cur.Left, ch)
187
214
}
188
215
}
189
216
```
0 commit comments