File tree 6 files changed +227
-0
lines changed
0124.Binary Tree Maximum Path Sum
0199.Binary Tree Right Side View
6 files changed +227
-0
lines changed Original file line number Diff line number Diff line change @@ -259,6 +259,49 @@ public class Solution {
259
259
}
260
260
```
261
261
262
+ ### ** Rust**
263
+
264
+ ``` rust
265
+ // Definition for a binary tree node.
266
+ // #[derive(Debug, PartialEq, Eq)]
267
+ // pub struct TreeNode {
268
+ // pub val: i32,
269
+ // pub left: Option<Rc<RefCell<TreeNode>>>,
270
+ // pub right: Option<Rc<RefCell<TreeNode>>>,
271
+ // }
272
+ //
273
+ // impl TreeNode {
274
+ // #[inline]
275
+ // pub fn new(val: i32) -> Self {
276
+ // TreeNode {
277
+ // val,
278
+ // left: None,
279
+ // right: None
280
+ // }
281
+ // }
282
+ // }
283
+ use std :: rc :: Rc ;
284
+ use std :: cell :: RefCell ;
285
+ impl Solution {
286
+ fn dfs (root : & Option <Rc <RefCell <TreeNode >>>, res : & mut i32 ) -> i32 {
287
+ if root . is_none () {
288
+ return 0 ;
289
+ }
290
+ let node = root . as_ref (). unwrap (). borrow ();
291
+ let left = 0. max (Self :: dfs (& node . left, res ));
292
+ let right = 0. max (Self :: dfs (& node . right, res ));
293
+ * res = (node . val + left + right ). max (* res );
294
+ node . val + left . max (right )
295
+ }
296
+
297
+ pub fn max_path_sum (root : Option <Rc <RefCell <TreeNode >>>) -> i32 {
298
+ let mut res = - 1000 ;
299
+ Self :: dfs (& root , & mut res );
300
+ res
301
+ }
302
+ }
303
+ ```
304
+
262
305
### ** ...**
263
306
264
307
```
Original file line number Diff line number Diff line change @@ -242,6 +242,49 @@ public class Solution {
242
242
}
243
243
```
244
244
245
+ ### ** Rust**
246
+
247
+ ``` rust
248
+ // Definition for a binary tree node.
249
+ // #[derive(Debug, PartialEq, Eq)]
250
+ // pub struct TreeNode {
251
+ // pub val: i32,
252
+ // pub left: Option<Rc<RefCell<TreeNode>>>,
253
+ // pub right: Option<Rc<RefCell<TreeNode>>>,
254
+ // }
255
+ //
256
+ // impl TreeNode {
257
+ // #[inline]
258
+ // pub fn new(val: i32) -> Self {
259
+ // TreeNode {
260
+ // val,
261
+ // left: None,
262
+ // right: None
263
+ // }
264
+ // }
265
+ // }
266
+ use std :: rc :: Rc ;
267
+ use std :: cell :: RefCell ;
268
+ impl Solution {
269
+ fn dfs (root : & Option <Rc <RefCell <TreeNode >>>, res : & mut i32 ) -> i32 {
270
+ if root . is_none () {
271
+ return 0 ;
272
+ }
273
+ let node = root . as_ref (). unwrap (). borrow ();
274
+ let left = 0. max (Self :: dfs (& node . left, res ));
275
+ let right = 0. max (Self :: dfs (& node . right, res ));
276
+ * res = (node . val + left + right ). max (* res );
277
+ node . val + left . max (right )
278
+ }
279
+
280
+ pub fn max_path_sum (root : Option <Rc <RefCell <TreeNode >>>) -> i32 {
281
+ let mut res = - 1000 ;
282
+ Self :: dfs (& root , & mut res );
283
+ res
284
+ }
285
+ }
286
+ ```
287
+
245
288
### ** ...**
246
289
247
290
```
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:: rc:: Rc ;
20
+ use std:: cell:: RefCell ;
21
+ impl Solution {
22
+ fn dfs ( root : & Option < Rc < RefCell < TreeNode > > > , res : & mut i32 ) -> i32 {
23
+ if root. is_none ( ) {
24
+ return 0 ;
25
+ }
26
+ let node = root. as_ref ( ) . unwrap ( ) . borrow ( ) ;
27
+ let left = 0 . max ( Self :: dfs ( & node. left , res) ) ;
28
+ let right = 0 . max ( Self :: dfs ( & node. right , res) ) ;
29
+ * res = ( node. val + left + right) . max ( * res) ;
30
+ node. val + left. max ( right)
31
+ }
32
+
33
+ pub fn max_path_sum ( root : Option < Rc < RefCell < TreeNode > > > ) -> i32 {
34
+ let mut res = -1000 ;
35
+ Self :: dfs ( & root, & mut res) ;
36
+ res
37
+ }
38
+ }
Original file line number Diff line number Diff line change @@ -193,6 +193,42 @@ func rightSideView(root *TreeNode) []int {
193
193
}
194
194
```
195
195
196
+ ### ** TypeScript**
197
+
198
+ ``` ts
199
+ /**
200
+ * Definition for a binary tree node.
201
+ * class TreeNode {
202
+ * val: number
203
+ * left: TreeNode | null
204
+ * right: TreeNode | null
205
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
206
+ * this.val = (val===undefined ? 0 : val)
207
+ * this.left = (left===undefined ? null : left)
208
+ * this.right = (right===undefined ? null : right)
209
+ * }
210
+ * }
211
+ */
212
+
213
+ function rightSideView(root : TreeNode | null ): number [] {
214
+ const res = [];
215
+ if (root == null ) {
216
+ return res ;
217
+ }
218
+ const queue = [root ];
219
+ while (queue .length !== 0 ) {
220
+ const n = queue .length ;
221
+ res .push (queue [n - 1 ].val );
222
+ for (let i = 0 ; i < n ; i ++ ) {
223
+ const { left, right } = queue .shift ();
224
+ left && queue .push (left );
225
+ right && queue .push (right );
226
+ }
227
+ }
228
+ return res ;
229
+ }
230
+ ```
231
+
196
232
### ** Rust**
197
233
198
234
``` rust
Original file line number Diff line number Diff line change @@ -179,6 +179,42 @@ func rightSideView(root *TreeNode) []int {
179
179
}
180
180
```
181
181
182
+ ### ** TypeScript**
183
+
184
+ ``` ts
185
+ /**
186
+ * Definition for a binary tree node.
187
+ * class TreeNode {
188
+ * val: number
189
+ * left: TreeNode | null
190
+ * right: TreeNode | null
191
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
192
+ * this.val = (val===undefined ? 0 : val)
193
+ * this.left = (left===undefined ? null : left)
194
+ * this.right = (right===undefined ? null : right)
195
+ * }
196
+ * }
197
+ */
198
+
199
+ function rightSideView(root : TreeNode | null ): number [] {
200
+ const res = [];
201
+ if (root == null ) {
202
+ return res ;
203
+ }
204
+ const queue = [root ];
205
+ while (queue .length !== 0 ) {
206
+ const n = queue .length ;
207
+ res .push (queue [n - 1 ].val );
208
+ for (let i = 0 ; i < n ; i ++ ) {
209
+ const { left, right } = queue .shift ();
210
+ left && queue .push (left );
211
+ right && queue .push (right );
212
+ }
213
+ }
214
+ return res ;
215
+ }
216
+ ```
217
+
182
218
### ** Rust**
183
219
184
220
``` rust
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * class TreeNode {
4
+ * val: number
5
+ * left: TreeNode | null
6
+ * right: TreeNode | null
7
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8
+ * this.val = (val===undefined ? 0 : val)
9
+ * this.left = (left===undefined ? null : left)
10
+ * this.right = (right===undefined ? null : right)
11
+ * }
12
+ * }
13
+ */
14
+
15
+ function rightSideView ( root : TreeNode | null ) : number [ ] {
16
+ const res = [ ] ;
17
+ if ( root == null ) {
18
+ return res ;
19
+ }
20
+ const queue = [ root ] ;
21
+ while ( queue . length !== 0 ) {
22
+ const n = queue . length ;
23
+ res . push ( queue [ n - 1 ] . val ) ;
24
+ for ( let i = 0 ; i < n ; i ++ ) {
25
+ const { left, right } = queue . shift ( ) ;
26
+ left && queue . push ( left ) ;
27
+ right && queue . push ( right ) ;
28
+ }
29
+ }
30
+ return res ;
31
+ }
You can’t perform that action at this time.
0 commit comments