File tree 3 files changed +193
-0
lines changed
solution/1300-1399/1305.All Elements in Two Binary Search Trees
3 files changed +193
-0
lines changed Original file line number Diff line number Diff line change @@ -244,6 +244,72 @@ func getAllElements(root1 *TreeNode, root2 *TreeNode) []int {
244
244
}
245
245
```
246
246
247
+ ### ** Rust**
248
+
249
+ ``` rust
250
+ // Definition for a binary tree node.
251
+ // #[derive(Debug, PartialEq, Eq)]
252
+ // pub struct TreeNode {
253
+ // pub val: i32,
254
+ // pub left: Option<Rc<RefCell<TreeNode>>>,
255
+ // pub right: Option<Rc<RefCell<TreeNode>>>,
256
+ // }
257
+ //
258
+ // impl TreeNode {
259
+ // #[inline]
260
+ // pub fn new(val: i32) -> Self {
261
+ // TreeNode {
262
+ // val,
263
+ // left: None,
264
+ // right: None
265
+ // }
266
+ // }
267
+ // }
268
+ use std :: cell :: RefCell ;
269
+ use std :: rc :: Rc ;
270
+ impl Solution {
271
+ pub fn get_all_elements (
272
+ root1 : Option <Rc <RefCell <TreeNode >>>,
273
+ root2 : Option <Rc <RefCell <TreeNode >>>,
274
+ ) -> Vec <i32 > {
275
+ fn dfs (root : & Option <Rc <RefCell <TreeNode >>>, t : & mut Vec <i32 >) {
276
+ if let Some (root ) = root {
277
+ dfs (& root . borrow (). left, t );
278
+ t . push (root . borrow (). val);
279
+ dfs (& root . borrow (). right, t );
280
+ }
281
+ }
282
+
283
+ let mut t1 = Vec :: new ();
284
+ let mut t2 = Vec :: new ();
285
+ dfs (& root1 , & mut t1 );
286
+ dfs (& root2 , & mut t2 );
287
+
288
+ let mut ans = Vec :: new ();
289
+ let mut i = 0 ;
290
+ let mut j = 0 ;
291
+ while i < t1 . len () && j < t2 . len () {
292
+ if t1 [i ] < t2 [j ] {
293
+ ans . push (t1 [i ]);
294
+ i += 1 ;
295
+ } else {
296
+ ans . push (t2 [j ]);
297
+ j += 1 ;
298
+ }
299
+ }
300
+ while i < t1 . len () {
301
+ ans . push (t1 [i ]);
302
+ i += 1 ;
303
+ }
304
+ while j < t2 . len () {
305
+ ans . push (t2 [j ]);
306
+ j += 1 ;
307
+ }
308
+ ans
309
+ }
310
+ }
311
+ ```
312
+
247
313
### ** ...**
248
314
249
315
```
Original file line number Diff line number Diff line change @@ -228,6 +228,72 @@ func getAllElements(root1 *TreeNode, root2 *TreeNode) []int {
228
228
}
229
229
```
230
230
231
+ ### ** Rust**
232
+
233
+ ``` rust
234
+ // Definition for a binary tree node.
235
+ // #[derive(Debug, PartialEq, Eq)]
236
+ // pub struct TreeNode {
237
+ // pub val: i32,
238
+ // pub left: Option<Rc<RefCell<TreeNode>>>,
239
+ // pub right: Option<Rc<RefCell<TreeNode>>>,
240
+ // }
241
+ //
242
+ // impl TreeNode {
243
+ // #[inline]
244
+ // pub fn new(val: i32) -> Self {
245
+ // TreeNode {
246
+ // val,
247
+ // left: None,
248
+ // right: None
249
+ // }
250
+ // }
251
+ // }
252
+ use std :: cell :: RefCell ;
253
+ use std :: rc :: Rc ;
254
+ impl Solution {
255
+ pub fn get_all_elements (
256
+ root1 : Option <Rc <RefCell <TreeNode >>>,
257
+ root2 : Option <Rc <RefCell <TreeNode >>>,
258
+ ) -> Vec <i32 > {
259
+ fn dfs (root : & Option <Rc <RefCell <TreeNode >>>, t : & mut Vec <i32 >) {
260
+ if let Some (root ) = root {
261
+ dfs (& root . borrow (). left, t );
262
+ t . push (root . borrow (). val);
263
+ dfs (& root . borrow (). right, t );
264
+ }
265
+ }
266
+
267
+ let mut t1 = Vec :: new ();
268
+ let mut t2 = Vec :: new ();
269
+ dfs (& root1 , & mut t1 );
270
+ dfs (& root2 , & mut t2 );
271
+
272
+ let mut ans = Vec :: new ();
273
+ let mut i = 0 ;
274
+ let mut j = 0 ;
275
+ while i < t1 . len () && j < t2 . len () {
276
+ if t1 [i ] < t2 [j ] {
277
+ ans . push (t1 [i ]);
278
+ i += 1 ;
279
+ } else {
280
+ ans . push (t2 [j ]);
281
+ j += 1 ;
282
+ }
283
+ }
284
+ while i < t1 . len () {
285
+ ans . push (t1 [i ]);
286
+ i += 1 ;
287
+ }
288
+ while j < t2 . len () {
289
+ ans . push (t2 [j ]);
290
+ j += 1 ;
291
+ }
292
+ ans
293
+ }
294
+ }
295
+ ```
296
+
231
297
### ** ...**
232
298
233
299
```
Original file line number Diff line number Diff line change
1
+ // Definition for a binary tree node.
2
+ // #[derive(Debug, PartialEq, Eq)]
3
+ // pub struct TreeNode {
4
+ // pub val: i32,
5
+ // pub left: Option<Rc<RefCell<TreeNode>>>,
6
+ // pub right: Option<Rc<RefCell<TreeNode>>>,
7
+ // }
8
+ //
9
+ // impl TreeNode {
10
+ // #[inline]
11
+ // pub fn new(val: i32) -> Self {
12
+ // TreeNode {
13
+ // val,
14
+ // left: None,
15
+ // right: None
16
+ // }
17
+ // }
18
+ // }
19
+ use std:: cell:: RefCell ;
20
+ use std:: rc:: Rc ;
21
+ impl Solution {
22
+ pub fn get_all_elements (
23
+ root1 : Option < Rc < RefCell < TreeNode > > > ,
24
+ root2 : Option < Rc < RefCell < TreeNode > > > ,
25
+ ) -> Vec < i32 > {
26
+ fn dfs ( root : & Option < Rc < RefCell < TreeNode > > > , t : & mut Vec < i32 > ) {
27
+ if let Some ( root) = root {
28
+ dfs ( & root. borrow ( ) . left , t) ;
29
+ t. push ( root. borrow ( ) . val ) ;
30
+ dfs ( & root. borrow ( ) . right , t) ;
31
+ }
32
+ }
33
+
34
+ let mut t1 = Vec :: new ( ) ;
35
+ let mut t2 = Vec :: new ( ) ;
36
+ dfs ( & root1, & mut t1) ;
37
+ dfs ( & root2, & mut t2) ;
38
+
39
+ let mut ans = Vec :: new ( ) ;
40
+ let mut i = 0 ;
41
+ let mut j = 0 ;
42
+ while i < t1. len ( ) && j < t2. len ( ) {
43
+ if t1[ i] < t2[ j] {
44
+ ans. push ( t1[ i] ) ;
45
+ i += 1 ;
46
+ } else {
47
+ ans. push ( t2[ j] ) ;
48
+ j += 1 ;
49
+ }
50
+ }
51
+ while i < t1. len ( ) {
52
+ ans. push ( t1[ i] ) ;
53
+ i += 1 ;
54
+ }
55
+ while j < t2. len ( ) {
56
+ ans. push ( t2[ j] ) ;
57
+ j += 1 ;
58
+ }
59
+ ans
60
+ }
61
+ }
You can’t perform that action at this time.
0 commit comments