File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
problems/second_minimum_node_in_a_binary_tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed 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
+ pub fn find_second_minimum_value ( root : Option < Rc < RefCell < TreeNode > > > ) -> i32 {
23
+ let mut v = Vec :: new ( ) ;
24
+ traverse ( & root, & mut v) ;
25
+ v. sort ( ) ;
26
+ v. dedup ( ) ;
27
+ match v. get ( 1 ) {
28
+ None => return -1 ,
29
+ Some ( x) => * x,
30
+ }
31
+ }
32
+ }
33
+
34
+ fn traverse ( root : & Option < Rc < RefCell < TreeNode > > > , v : & mut Vec < i32 > ) {
35
+ if let Some ( val) = root {
36
+ traverse ( & val. borrow ( ) . left , v) ;
37
+ v. push ( val. borrow ( ) . val ) ;
38
+ traverse ( & val. borrow ( ) . right , v) ;
39
+ }
40
+ }
You can’t perform that action at this time.
0 commit comments