Last active
August 29, 2015 14:07
-
-
Save dyng/5c195645cce231cc4d80 to your computer and use it in GitHub Desktop.
Compare tree in Rust
This file contains hidden or 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
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