Created
May 10, 2015 17:11
-
-
Save HDegano/c13e95461394a8e2065b to your computer and use it in GitHub Desktop.
Check if two binary trees are equal
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
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