mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0257.二叉树的所有路径.md
This commit is contained in:
@ -794,6 +794,34 @@ object Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
rust:
|
||||
|
||||
```rust
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
impl Solution {
|
||||
pub fn binary_tree_paths(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<String> {
|
||||
let mut res = vec![];
|
||||
Self::recur(&root, String::from(""), &mut res);
|
||||
res
|
||||
}
|
||||
pub fn recur(node: &Option<Rc<RefCell<TreeNode>>>, mut path: String, res: &mut Vec<String>) {
|
||||
let r = node.as_ref().unwrap().borrow();
|
||||
path += format!("{}", r.val).as_str();
|
||||
if r.left.is_none() && r.right.is_none() {
|
||||
res.push(path.to_string());
|
||||
return;
|
||||
}
|
||||
if r.left.is_some() {
|
||||
Self::recur(&r.left, path.clone() + "->", res);
|
||||
}
|
||||
if r.right.is_some() {
|
||||
Self::recur(&r.right, path + "->", res);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
Reference in New Issue
Block a user