From 59e91d5da4e7b180cf4651675f64a7e57fb5a7ae Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 25 Feb 2024 14:24:54 +0800 Subject: [PATCH 1/3] 20240225 Finished --- src/problem/mod.rs | 3 +- ...common_ancestor_of_a_binary_search_tree.rs | 58 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/problem/p235_lowest_common_ancestor_of_a_binary_search_tree.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 0ce5d94..3af29c7 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -51,4 +51,5 @@ mod p102_binary_tree_level_order_traversal; 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; \ No newline at end of file +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 diff --git a/src/problem/p235_lowest_common_ancestor_of_a_binary_search_tree.rs b/src/problem/p235_lowest_common_ancestor_of_a_binary_search_tree.rs new file mode 100644 index 0000000..fe3d894 --- /dev/null +++ b/src/problem/p235_lowest_common_ancestor_of_a_binary_search_tree.rs @@ -0,0 +1,58 @@ +/** + * [235] Lowest Common Ancestor of a 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>>, +// 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 lowest_common_ancestor(root: Option>>, p: Option>>, q: Option>>) -> Option>> { + let (root, p, q) = (root?, p?, q?); + let mut ancestor = root; + + loop { + ancestor = if p.borrow().val < ancestor.borrow().val && q.borrow().val < ancestor.borrow().val { + Rc::clone(&ancestor.borrow().left.as_ref().unwrap()) + } else if (p.borrow().val > ancestor.borrow().val && q.borrow().val > ancestor.borrow().val ) { + Rc::clone(&ancestor.borrow().right.as_ref().unwrap()) + } else { + break; + } + } + + Some(ancestor) + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_235() { + } +} From 5ddfa86ac8a57ad7179fac89035cc8b129f0fa4d Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 25 Feb 2024 15:15:14 +0800 Subject: [PATCH 2/3] 20240223 20240224 Finished --- src/problem/mod.rs | 4 +- ...t_nodes_queries_in_a_binary_search_tree.rs | 84 +++++++++++++++++++ .../p2583_kth_largest_sum_in_a_binary_tree.rs | 80 ++++++++++++++++++ 3 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2476_closest_nodes_queries_in_a_binary_search_tree.rs create mode 100644 src/problem/p2583_kth_largest_sum_in_a_binary_tree.rs 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() { + } +} From d251733113c5a846cf626441221c782c0edbc94f Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 26 Feb 2024 09:16:06 +0800 Subject: [PATCH 3/3] 20240226 Finished --- src/problem/mod.rs | 3 +- src/problem/p938_range_sum_of_bst.rs | 79 ++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/problem/p938_range_sum_of_bst.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 49f8fe8..a4ae5ee 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -54,4 +54,5 @@ 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; 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 +mod p2476_closest_nodes_queries_in_a_binary_search_tree; +mod p938_range_sum_of_bst; \ No newline at end of file diff --git a/src/problem/p938_range_sum_of_bst.rs b/src/problem/p938_range_sum_of_bst.rs new file mode 100644 index 0000000..dd1b277 --- /dev/null +++ b/src/problem/p938_range_sum_of_bst.rs @@ -0,0 +1,79 @@ +/** + * [938] Range Sum of 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>>, +// 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; +use std::collections::VecDeque; +impl Solution { + pub fn range_sum_bst(root: Option>>, low: i32, high: i32) -> i32 { + let root = if let Some(r) = root { + r + } else { + return 0; + }; + + let mut sum = 0; + + let mut deque = VecDeque::new(); + deque.push_back(root); + + while !deque.is_empty() { + let node = deque.pop_front().unwrap(); + + if node.borrow().val >= low && node.borrow().val <= high { + sum += node.borrow().val; + } + + if node.borrow().val > low { + if let Some(left) = &node.borrow().left { + deque.push_back(Rc::clone(left)); + } + } + + if node.borrow().val < high { + if let Some(right) = &node.borrow().right { + deque.push_back(Rc::clone(right)); + } + } + } + + sum + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_938() { + assert_eq!(Solution::range_sum_bst( + to_tree(vec![Some(10), Some(5), Some(15), Some(3), Some(7), None, Some(18)]), 7, 15), 32); + } +}