make dfs same as c/c++ and other small improvement (#1543)

This commit is contained in:
rongyi
2024-10-31 21:24:56 +08:00
committed by GitHub
parent 7d708b4fce
commit 68a61f23d5
3 changed files with 38 additions and 29 deletions

View File

@ -49,18 +49,11 @@ impl ArrayBinaryTree {
/* 层序遍历 */
fn level_order(&self) -> Vec<i32> {
let mut res = vec![];
// 直接遍历数组
for i in 0..self.size() {
if let Some(val) = self.val(i) {
res.push(val)
}
}
res
self.tree.iter().filter_map(|&x| x).collect()
}
/* 深度优先遍历 */
fn dfs(&self, i: i32, order: &str, res: &mut Vec<i32>) {
fn dfs(&self, i: i32, order: &'static str, res: &mut Vec<i32>) {
if self.val(i).is_none() {
return;
}