Created
March 1, 2015 19:01
-
-
Save dmnugent80/c0a56ba9ce13ff23c054 to your computer and use it in GitHub Desktop.
Is Same Binary Tree
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
/** | |
* Definition for binary tree | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode(int x) { val = x; } | |
* } | |
*/ | |
public class Solution { | |
public boolean isSameTree(TreeNode p, TreeNode q) { | |
if (p == null && q == null) return true; | |
return isSameTreeHelper(p, q); | |
} | |
public boolean isSameTreeHelper(TreeNode p, TreeNode q){ | |
if (p == null && q == null) return true; | |
if (p != null && q != null){ | |
if (p.val != q.val){ | |
return false; | |
} | |
if (!isSameTreeHelper(p.left, q.left)){ | |
return false; | |
} | |
return (isSameTreeHelper(p.right, q.right)); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment