Skip to content

Commit

Permalink
[algorithms]添加bfs的层序遍历
Browse files Browse the repository at this point in the history
  • Loading branch information
Trackerming committed Jul 16, 2024
1 parent 5d80e71 commit 63d56d3
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions algorithms/src/tree_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ where
return result;
}

pub fn bfs_level_order(root: &Rc<RefCell<TreeNode<i32>>>) -> Vec<i32> {
let mut que = VecDeque::new();
que.push_back(root.clone());
let mut results = Vec::new();
while let Some(node) = que.pop_front() {
results.push(node.borrow().val);
if let Some(left) = node.borrow().left.as_ref() {
que.push_back(left.clone());
}
if let Some(right) = node.borrow().right.as_ref() {
que.push_back(right.clone());
}
}
results
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -177,4 +193,13 @@ mod test {
println!("result: {:?}", result);
assert_eq!(result, vec![1, 7, 5, 12, 10, 8]);
}

#[test]
fn test_bfs() {
let tree: TreeLink<i32> =
tree!(8, tree!(5, tree!(1), tree!(7)), tree!(10, None, tree!(12)));
let results = bfs_level_order(&tree.clone().unwrap());
println!("results: {:?}", results);
assert_eq!(results, vec![8, 5, 10, 1, 7, 12]);
}
}

0 comments on commit 63d56d3

Please sign in to comment.