Skip to content

Commit

Permalink
finish minimum distance between bst nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
checky committed Mar 20, 2021
1 parent ae95806 commit 02cedd9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 16-50/minimum-distance-between-bst-nodes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "minimum-distance-between-bst-nodes"
version = "0.1.0"
authors = ["checky <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
56 changes: 56 additions & 0 deletions 16-50/minimum-distance-between-bst-nodes/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::cell::RefCell;
use std::rc::Rc;

#[derive(Debug, PartialEq, Eq)]
struct TreeNode {
val: i32,
left: Option<Rc<RefCell<TreeNode>>>,
right: Option<Rc<RefCell<TreeNode>>>,
}

impl TreeNode {
#[inline]
fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None,
}
}
}

struct Solution {}

impl Solution {
fn min_diff_in_bst_loop(node: Option<&Rc<RefCell<TreeNode>>>, vec: &mut Vec<i32>) {
if let Some(x) = node {
let v = x.borrow();
Solution::min_diff_in_bst_loop(v.left.as_ref(), vec);
vec.push(v.val);
Solution::min_diff_in_bst_loop(v.right.as_ref(), vec);
}
}

pub fn min_diff_in_bst(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
let mut vec: Vec<i32> = Vec::new();
Solution::min_diff_in_bst_loop(root.as_ref(), &mut vec);
let mut result: i32 = i32::MAX;
for i in 1..vec.len() {
let t = vec[i] - vec[i - 1];
if t < result {
result = t;
}
}
result
}
}

fn main() {
let mut node = TreeNode::new(3);
node.left = Some(Rc::new(RefCell::new(TreeNode::new(1))));
node.right = Some(Rc::new(RefCell::new(TreeNode::new(5))));
println!(
"Minimum distance: {}",
Solution::min_diff_in_bst(Some(Rc::new(RefCell::new(node))))
);
}

0 comments on commit 02cedd9

Please sign in to comment.