Skip to content

Instantly share code, notes, and snippets.

@dyng
Last active August 29, 2015 14:07
Show Gist options
  • Save dyng/5c195645cce231cc4d80 to your computer and use it in GitHub Desktop.
Save dyng/5c195645cce231cc4d80 to your computer and use it in GitHub Desktop.
Compare tree in Rust
fn is_same_tree(p: &Tree<int>, q: &Tree<int>) -> bool {
match (*p, *q) { // error: cannot move out of dereference of `&`-pointer
(None, None) => true,
(Some(ref a), Some(ref b)) => {
if a.val == b.val
&& is_same_tree(&a.left, &b.left)
&& is_same_tree(&a.right, &b.right){
true
} else {
false
}
},
(_, _) => false,
}
}
type Tree<T> = Option<Box<TreeNode<T>>>;
struct TreeNode<T> {
val: T,
left: Tree<T>,
right: Tree<T>,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment