Skip to content

Commit

Permalink
binary_trees: non-tail-recursive item_check
Browse files Browse the repository at this point in the history
This matches the current submission and follows the rules as laid out by Isaac.
  • Loading branch information
cristicbz authored and TeXitoi committed Mar 11, 2017
1 parent c0c0056 commit cb89ea9
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/binary_trees.rs
Expand Up @@ -18,14 +18,11 @@ struct Tree<'a> {
}

fn item_check(tree: &Tree) -> i32 {
fn check(sum: i32, tree: &Tree) -> i32 {
if let Some((left, right)) = tree.children {
check(sum + tree.item - item_check(right), left)
} else {
sum + tree.item
}
if let Some((left, right)) = tree.children {
tree.item - item_check(right) + item_check(left)
} else {
tree.item
}
check(0, tree)
}

fn bottom_up_tree<'r>(arena: &'r Arena<Tree<'r>>, item: i32, depth: i32)
Expand Down

0 comments on commit cb89ea9

Please sign in to comment.