-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters