Skip to content

Commit c9bb5be

Browse files
committed
Sync LeetCode submission - Second Minimum Node In a Binary Tree (rust)
1 parent 1f0bfe3 commit c9bb5be

File tree

1 file changed

+40
-0
lines changed
  • problems/second_minimum_node_in_a_binary_tree

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

0 commit comments

Comments
 (0)