20240709 Finished

This commit is contained in:
jackfiled 2024-07-09 09:35:27 +08:00
parent 75e3159aac
commit 8b4dc6823d
2 changed files with 72 additions and 1 deletions

View File

@ -170,4 +170,5 @@ mod p77_combinations;
mod p46_permutations;
mod p39_combination_sum;
mod p22_generate_parentheses;
mod p79_word_search;
mod p79_word_search;
mod p108_convert_sorted_array_to_binary_search_tree;

View File

@ -0,0 +1,70 @@
/**
* [108] Convert Sorted Array to Binary Search Tree
*/
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 {
pub fn sorted_array_to_bst(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
Some(Self::array_to_bst(&nums[..]))
}
fn array_to_bst(array: &[i32]) -> Rc<RefCell<TreeNode>> {
let length = array.len();
if length == 1 {
return Rc::new(RefCell::new(TreeNode::new(array[0])));
}
let middle = length / 2;
let node = Rc::new(RefCell::new(TreeNode::new(array[middle])));
if middle != 0 {
// 左边
let left = &array[..middle];
node.borrow_mut().left = Some(Self::array_to_bst(left));
}
if middle != length - 1 {
let right = &array[middle + 1..];
node.borrow_mut().right = Some(Self::array_to_bst(right));
}
return node;
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_108() {
}
}