@@ -219,6 +219,82 @@ func inorder(ctx context.Context, cur *TreeNode, ch chan<- int) {
219
219
}
220
220
```
221
221
222
+ ### ** TypeScript**
223
+
224
+ ``` ts
225
+ /**
226
+ * Definition for a binary tree node.
227
+ * class TreeNode {
228
+ * val: number
229
+ * left: TreeNode | null
230
+ * right: TreeNode | null
231
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
232
+ * this.val = (val===undefined ? 0 : val)
233
+ * this.left = (left===undefined ? null : left)
234
+ * this.right = (right===undefined ? null : right)
235
+ * }
236
+ * }
237
+ */
238
+
239
+ function kthLargest(root : TreeNode | null , k : number ): number {
240
+ let res = 0 ;
241
+ const dfs = (root : TreeNode | null ) => {
242
+ if (root == null ) {
243
+ return ;
244
+ }
245
+ dfs (root .right );
246
+ if (-- k === 0 ) {
247
+ res = root .val ;
248
+ return ;
249
+ }
250
+ dfs (root .left );
251
+ };
252
+ dfs (root );
253
+ return res ;
254
+ }
255
+ ```
256
+
257
+ ### ** Rust**
258
+
259
+ ``` rust
260
+ // Definition for a binary tree node.
261
+ // #[derive(Debug, PartialEq, Eq)]
262
+ // pub struct TreeNode {
263
+ // pub val: i32,
264
+ // pub left: Option<Rc<RefCell<TreeNode>>>,
265
+ // pub right: Option<Rc<RefCell<TreeNode>>>,
266
+ // }
267
+ //
268
+ // impl TreeNode {
269
+ // #[inline]
270
+ // pub fn new(val: i32) -> Self {
271
+ // TreeNode {
272
+ // val,
273
+ // left: None,
274
+ // right: None
275
+ // }
276
+ // }
277
+ // }
278
+ use std :: rc :: Rc ;
279
+ use std :: cell :: RefCell ;
280
+
281
+ impl Solution {
282
+ fn dfs (root : & Option <Rc <RefCell <TreeNode >>>, arr : & mut Vec <i32 >) {
283
+ if let Some (node ) = root {
284
+ let node = node . borrow ();
285
+ Solution :: dfs (& node . right, arr );
286
+ arr . push (node . val);
287
+ Solution :: dfs (& node . left, arr )
288
+ }
289
+ }
290
+ pub fn kth_largest (root : Option <Rc <RefCell <TreeNode >>>, k : i32 ) -> i32 {
291
+ let mut arr = vec! [];
292
+ Solution :: dfs (& root , & mut arr );
293
+ arr [(k - 1 ) as usize ]
294
+ }
295
+ }
296
+ ```
297
+
222
298
### ** ...**
223
299
224
300
```
0 commit comments