20240617 Finished
This commit is contained in:
parent
ab3380bf47
commit
1965c6fabd
|
@ -150,4 +150,5 @@ mod p124_binary_tree_maximum_path_sum;
|
|||
mod p173_binary_search_tree_iterator;
|
||||
mod p222_count_complete_tree_nodes;
|
||||
mod p199_binary_tree_right_side_view;
|
||||
mod p637_average_of_levels_in_binary_tree;
|
||||
mod p637_average_of_levels_in_binary_tree;
|
||||
mod p530_minimum_absolute_difference_in_bst;
|
69
src/problem/p530_minimum_absolute_difference_in_bst.rs
Normal file
69
src/problem/p530_minimum_absolute_difference_in_bst.rs
Normal file
|
@ -0,0 +1,69 @@
|
|||
/**
|
||||
* [530] Minimum Absolute Difference in BST
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
use crate::util::tree::{TreeNode, to_tree};
|
||||
|
||||
// submission codes start here
|
||||
|
||||
// Definition for a binary tree node.
|
||||
// #[derive(Debug, PartialEq, Eq)]
|
||||
// pub struct TreeNode {
|
||||
// pub val: i32,
|
||||
// pub left: Option<Rc<RefCell<TreeNode>>>,
|
||||
// pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
// }
|
||||
//
|
||||
// impl TreeNode {
|
||||
// #[inline]
|
||||
// pub fn new(val: i32) -> Self {
|
||||
// TreeNode {
|
||||
// val,
|
||||
// left: None,
|
||||
// right: None
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
impl Solution {
|
||||
fn inorder_iterate(node: &Rc<RefCell<TreeNode>>, arrap: &mut Vec<i32>) {
|
||||
if let Some(left) = node.borrow().left.as_ref() {
|
||||
Self::inorder_iterate(left, arrap);
|
||||
}
|
||||
|
||||
arrap.push(node.borrow().val);
|
||||
|
||||
if let Some(right) = node.borrow().right.as_ref() {
|
||||
Self::inorder_iterate(right, arrap);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_minimum_difference(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||
let mut array = vec![];
|
||||
|
||||
if let Some(root) = root {
|
||||
Self::inorder_iterate(&root, &mut array);
|
||||
}
|
||||
|
||||
let mut result = i32::MAX;
|
||||
|
||||
for i in 1..array.len() {
|
||||
result = result.min(array[i] - array[i - 1]);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_530() {
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user