# My java ans with simple logic judgement and recursive

• ``````/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
//Based on the structure of the bi-tree, this solution mainly compares
//three nodes of the two trees each time. Firstly, test the parent nodes.

if ((p == null && q != null) || (q == null && p != null)) return false;
else if (p == null && q == null)
return true;// omit situation with only two null tree root nodes
else if (p.val != q.val) return false;
//if parent nodes are OK, then

else if (p.left == null && q.left ==null && p.right == null && q.right == null)
return true;
else if ((p.left == null && q.left == null) && (p.right != null && q.right != null))
return isSameTree(p.right, q.right);
else if ((q.left != null && p.left != null) && (q.right == null && p.right == null))
return isSameTree(p.left, q.left);
else
return ((isSameTree(p.left, q.left)) && (isSameTree(p.right, q.right)));
}
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.