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

This commit is contained in:
fw_qaq
2022-12-09 20:15:28 +08:00
committed by GitHub
parent 4341179d93
commit 26f01c3ef9

View File

@ -396,6 +396,30 @@ impl Solution {
} }
``` ```
迭代:
```rust
impl Solution {
pub fn convert_bst(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
let mut cur = root.clone();
let mut stack = vec![];
let mut pre = 0;
while !stack.is_empty() || cur.is_some() {
while let Some(node) = cur {
cur = node.borrow().right.clone();
stack.push(node);
}
if let Some(node) = stack.pop() {
pre += node.borrow().val;
node.borrow_mut().val = pre;
cur = node.borrow().left.clone();
}
}
root
}
}
```
<p align="center"> <p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">