20240223 20240224 Finished

This commit is contained in:
jackfiled 2024-02-25 15:15:14 +08:00
parent 59e91d5da4
commit 5ddfa86ac8
3 changed files with 167 additions and 1 deletions

View File

@ -52,4 +52,6 @@ mod p107_binary_tree_level_order_traversal_ii;
mod p103_binary_tree_zigzag_level_order_traversal; mod p103_binary_tree_zigzag_level_order_traversal;
mod p105_construct_binary_tree_from_preorder_and_inorder_traversal; mod p105_construct_binary_tree_from_preorder_and_inorder_traversal;
mod p106_construct_binary_tree_from_inorder_and_postorder_traversal; mod p106_construct_binary_tree_from_inorder_and_postorder_traversal;
mod p235_lowest_common_ancestor_of_a_binary_search_tree; mod p235_lowest_common_ancestor_of_a_binary_search_tree;
mod p2583_kth_largest_sum_in_a_binary_tree;
mod p2476_closest_nodes_queries_in_a_binary_search_tree;

View File

@ -0,0 +1,84 @@
/**
* [2476] Closest Nodes Queries in a Binary Search Tree
*/
pub struct Solution {}
use surf::url::UrlQuery;
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 closest_nodes(root: Option<Rc<RefCell<TreeNode>>>, queries: Vec<i32>) -> Vec<Vec<i32>> {
let mut array = Vec::new();
let mut stack = Vec::new();
let mut root = root;
while root.is_some() || !stack.is_empty() {
while let Some(r) = root {
stack.push(Rc::clone(&r));
root = r.borrow().left.clone();
}
root = stack.pop();
if let Some(r) = root {
array.push(r.borrow().val);
root = r.borrow().right.clone();
}
}
let mut result = Vec::with_capacity(queries.len());
for query in queries {
let pos = array.binary_search(&query);
match pos {
Ok(pos) => {
result.push(vec![array[pos], array[pos]]);
},
Err(pos) => {
if pos == 0 {
result.push(vec![-1, array[pos]]);
} else if pos == array.len() {
result.push(vec![array[pos - 1], -1])
} else {
result.push(vec![array[pos - 1], array[pos]]);
}
}
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2476() {
}
}

View File

@ -0,0 +1,80 @@
/**
* [2583] Kth Largest Sum in a Binary 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 kth_largest_level_sum(root: Option<Rc<RefCell<TreeNode>>>, k: i32) -> i64 {
let root = if let Some(r) = root {
r
} else {
return -1;
};
let k = k as usize;
let mut levels = Vec::new();
let mut level = vec![root];
while !level.is_empty() {
let mut new_level = Vec::new();
let mut sum = 0;
for node in level {
sum += node.borrow().val as i64;
if let Some(left) = &node.borrow().left {
new_level.push(Rc::clone(left));
}
if let Some(right) = &node.borrow().right {
new_level.push(Rc::clone(right));
}
}
levels.push(sum);
level = new_level;
}
if levels.len() < k {
return -1;
}
levels.sort_unstable();
levels[levels.len() - k]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2583() {
}
}