diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 3af29c7..49f8fe8 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -52,4 +52,6 @@ mod p107_binary_tree_level_order_traversal_ii; mod p103_binary_tree_zigzag_level_order_traversal; mod p105_construct_binary_tree_from_preorder_and_inorder_traversal; mod p106_construct_binary_tree_from_inorder_and_postorder_traversal; -mod p235_lowest_common_ancestor_of_a_binary_search_tree; \ No newline at end of file +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; \ No newline at end of file diff --git a/src/problem/p2476_closest_nodes_queries_in_a_binary_search_tree.rs b/src/problem/p2476_closest_nodes_queries_in_a_binary_search_tree.rs new file mode 100644 index 0000000..5bc72b9 --- /dev/null +++ b/src/problem/p2476_closest_nodes_queries_in_a_binary_search_tree.rs @@ -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>>, +// pub right: Option>>, +// } +// +// 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>>, queries: Vec) -> Vec> { + 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() { + } +} diff --git a/src/problem/p2583_kth_largest_sum_in_a_binary_tree.rs b/src/problem/p2583_kth_largest_sum_in_a_binary_tree.rs new file mode 100644 index 0000000..53aa03b --- /dev/null +++ b/src/problem/p2583_kth_largest_sum_in_a_binary_tree.rs @@ -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>>, +// pub right: Option>>, +// } +// +// 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>>, 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() { + } +}