Update 0538.把二叉搜索树转换为累加树.md

This commit is contained in:
fw_qaq
2022-12-09 20:02:58 +08:00
committed by GitHub
parent 5af63a5ab5
commit 4341179d93

View File

@ -371,6 +371,31 @@ object Solution {
}
```
## rust
递归:
```rust
impl Solution {
pub fn convert_bst(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
let mut pre = 0;
Self::traversal(&root, &mut pre);
root
}
pub fn traversal(cur: &Option<Rc<RefCell<TreeNode>>>, pre: &mut i32) {
if cur.is_none() {
return;
}
let mut node = cur.as_ref().unwrap().borrow_mut();
Self::traversal(&node.right, pre);
*pre += node.val;
node.val = *pre;
Self::traversal(&node.left, pre);
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">