Merge branch 'master' of https://git.rrricardo.top/jackfiled/leetcode
This commit is contained in:
commit
d55cc4e4cd
|
@ -52,4 +52,8 @@ 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;
|
||||
mod p2583_kth_largest_sum_in_a_binary_tree;
|
||||
mod p2476_closest_nodes_queries_in_a_binary_search_tree;
|
||||
mod p938_range_sum_of_bst;
|
||||
mod p889_construct_binary_tree_from_preorder_and_postorder_traversal;
|
|
@ -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<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 lowest_common_ancestor(root: Option<Rc<RefCell<TreeNode>>>, p: Option<Rc<RefCell<TreeNode>>>, q: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
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() {
|
||||
}
|
||||
}
|
|
@ -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() {
|
||||
}
|
||||
}
|
80
src/problem/p2583_kth_largest_sum_in_a_binary_tree.rs
Normal file
80
src/problem/p2583_kth_largest_sum_in_a_binary_tree.rs
Normal 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() {
|
||||
}
|
||||
}
|
79
src/problem/p938_range_sum_of_bst.rs
Normal file
79
src/problem/p938_range_sum_of_bst.rs
Normal file
|
@ -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<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;
|
||||
use std::collections::VecDeque;
|
||||
impl Solution {
|
||||
pub fn range_sum_bst(root: Option<Rc<RefCell<TreeNode>>>, 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user