Skip to content

Commit

Permalink
add: 从二叉搜索树到更大和树
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Dec 4, 2023
1 parent 24a61a7 commit e4bacbb
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

###

- [从二叉搜索树到更大和树](src/tree/binary_search_tree_to_greater_sum_tree.rs) [树, 深度优先搜索, 二叉搜索树, 二叉树]

- LeetCode 1038. 从二叉搜索树到更大和树 <https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree>

- [树节点的第 K 个祖先](src/tree/kth_ancestor_of_a_tree_node.rs) [树, 深度优先搜索, 广度优先搜索, 设计, 二分查找, 动态规划]

- LeetCode 1483. 树节点的第 K 个祖先 <https://leetcode.cn/problems/kth-ancestor-of-a-tree-node>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions src/libs/tree_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,37 @@ impl TreeNode {
right: None,
}
}

pub fn from_vec(values: Vec<Option<i32>>) -> Option<Rc<RefCell<Self>>> {
if values.is_empty() {
return None;
}

let root = Rc::new(RefCell::new(TreeNode::new(values[0].unwrap())));
let mut queue = std::collections::VecDeque::new();
queue.push_back(root.clone());

let mut i = 1;
while i < values.len() {
let parent = queue.pop_front().unwrap();

if i < values.len() && values[i].is_some() {
let left_child = Rc::new(RefCell::new(TreeNode::new(values[i].unwrap())));
parent.borrow_mut().left = Some(left_child.clone());
queue.push_back(left_child);
}
i += 1;

if i < values.len() && values[i].is_some() {
let right_child = Rc::new(RefCell::new(TreeNode::new(values[i].unwrap())));
parent.borrow_mut().right = Some(right_child.clone());
queue.push_back(right_child);
}
i += 1;
}

Some(root)
}
}

// fn create_tree(vec: &Vec<Option<i32>>, index: usize) -> Option<Rc<RefCell<TreeNode>>> {
Expand Down
38 changes: 38 additions & 0 deletions src/tree/binary_search_tree_to_greater_sum_tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 从二叉搜索树到更大和树
// https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree
// INLINE ../../images/tree/binary_search_tree_to_greater_sum_tree.jpeg

use std::{cell::RefCell, rc::Rc};

use crate::libs::tree_node::TreeNode;

pub struct Solution;

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
impl Solution {
pub fn bst_to_gst(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
let mut sum = 0;
let cloned_root = root.clone(); // 克隆root
Solution::bst_to_gst_helper(cloned_root, &mut sum);
root
}

fn bst_to_gst_helper(root: Option<Rc<RefCell<TreeNode>>>, sum: &mut i32) {
if let Some(node) = root {
Solution::bst_to_gst_helper(node.borrow().right.clone(), sum);
*sum += node.borrow().val;
node.borrow_mut().val = *sum;
Solution::bst_to_gst_helper(node.borrow().left.clone(), sum);
}
}
}
1 change: 1 addition & 0 deletions src/tree/binary_tree_inorder_traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use std::cell::RefCell;
use std::rc::Rc;

use crate::libs::tree_node::TreeNode;

impl Solution {
// 解决方法1: 循环法
pub fn inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
Expand Down
1 change: 1 addition & 0 deletions src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ pub mod maximum_difference_between_node_and_ancestor;
pub mod maximum_sum_bst_in_binary_tree;
pub mod time_needed_to_inform_all_employees;
pub mod kth_ancestor_of_a_tree_node;
pub mod binary_search_tree_to_greater_sum_tree;
62 changes: 62 additions & 0 deletions tests/tree/binary_search_tree_to_greater_sum_tree_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use rust_practice::{
libs::tree_node::TreeNode, tree::binary_search_tree_to_greater_sum_tree::Solution,
};

#[test]
fn test_tree_node() {
// 示例 1:
// 输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
// 输出:[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
// 构建输入的二叉树
let tree1 = TreeNode::from_vec(vec![
Some(4),
Some(1),
Some(6),
Some(0),
Some(2),
Some(5),
Some(7),
None,
None,
None,
Some(3),
None,
None,
None,
Some(8),
]);
// 调用函数
let result1 = Solution::bst_to_gst(tree1);
// 构建预期输出
let expected1 = TreeNode::from_vec(vec![
Some(30),
Some(36),
Some(21),
Some(36),
Some(35),
Some(26),
Some(15),
None,
None,
None,
Some(33),
None,
None,
None,
Some(8),
]);
// 断言
assert_eq!(result1, expected1);

// 示例 2:
// 输入:root = [0,null,1]
// 输出:[1,null,1]
// 构建输入的二叉树
let tree2 = TreeNode::from_vec(vec![Some(0), None, Some(1)]);
// 调用函数
let result2 = Solution::bst_to_gst(tree2);
// 构建预期输出
let expected2 = TreeNode::from_vec(vec![Some(1), None, Some(1)]);
// 断言
assert_eq!(result2, expected2);
}
1 change: 1 addition & 0 deletions tests/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ pub mod maximum_sum_bst_in_binary_tree_test;
pub mod insufficient_nodes_in_root_to_leaf_paths_test;
pub mod delete_nodes_and_return_forest_test;
pub mod kth_ancestor_of_a_tree_node_test;
pub mod binary_search_tree_to_greater_sum_tree_test;

0 comments on commit e4bacbb

Please sign in to comment.