Skip to content

Instantly share code, notes, and snippets.

@HDegano
Created May 10, 2015 17:11
Show Gist options
  • Save HDegano/c13e95461394a8e2065b to your computer and use it in GitHub Desktop.
Save HDegano/c13e95461394a8e2065b to your computer and use it in GitHub Desktop.
Check if two binary trees are equal
public class SameTree{
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null) return true;
if(p == null || q == null) return false;
if(p.val != q.val) return false;
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment