-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make dfs same as c/c++ and other small improvement (#1543)
- Loading branch information
Showing
3 changed files
with
38 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
* Author: xBLACKICEx ([email protected]) | ||
*/ | ||
|
||
use hello_algo_rust::include::{vec_to_tree, TreeNode, print_util}; | ||
use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode}; | ||
use hello_algo_rust::op_vec; | ||
|
||
use std::cell::RefCell; | ||
|
@@ -14,38 +14,54 @@ use std::rc::Rc; | |
fn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> { | ||
let mut result = vec![]; | ||
|
||
if let Some(node) = root { | ||
// 访问优先级:根节点 -> 左子树 -> 右子树 | ||
result.push(node.borrow().val); | ||
result.extend(pre_order(node.borrow().left.as_ref())); | ||
result.extend(pre_order(node.borrow().right.as_ref())); | ||
fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) { | ||
if let Some(node) = root { | ||
// 访问优先级:根节点 -> 左子树 -> 右子树 | ||
let node = node.borrow(); | ||
res.push(node.val); | ||
dfs(node.left.as_ref(), res); | ||
dfs(node.right.as_ref(), res); | ||
} | ||
} | ||
dfs(root, &mut result); | ||
|
||
result | ||
} | ||
|
||
/* 中序遍历 */ | ||
fn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> { | ||
let mut result = vec![]; | ||
|
||
if let Some(node) = root { | ||
// 访问优先级:左子树 -> 根节点 -> 右子树 | ||
result.extend(in_order(node.borrow().left.as_ref())); | ||
result.push(node.borrow().val); | ||
result.extend(in_order(node.borrow().right.as_ref())); | ||
fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) { | ||
if let Some(node) = root { | ||
// 访问优先级:左子树 -> 根节点 -> 右子树 | ||
let node = node.borrow(); | ||
dfs(node.left.as_ref(), res); | ||
res.push(node.val); | ||
dfs(node.right.as_ref(), res); | ||
} | ||
} | ||
dfs(root, &mut result); | ||
|
||
result | ||
} | ||
|
||
/* 后序遍历 */ | ||
fn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> { | ||
let mut result = vec![]; | ||
|
||
if let Some(node) = root { | ||
// 访问优先级:左子树 -> 右子树 -> 根节点 | ||
result.extend(post_order(node.borrow().left.as_ref())); | ||
result.extend(post_order(node.borrow().right.as_ref())); | ||
result.push(node.borrow().val); | ||
fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) { | ||
if let Some(node) = root { | ||
// 访问优先级:左子树 -> 右子树 -> 根节点 | ||
let node = node.borrow(); | ||
dfs(node.left.as_ref(), res); | ||
dfs(node.right.as_ref(), res); | ||
res.push(node.val); | ||
} | ||
} | ||
|
||
dfs(root, &mut result); | ||
|
||
result | ||
} | ||
|
||
|