Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
Tree is defined as :
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
And my solution is
public boolean isSameTree(TreeNode p, TreeNode q) {
if(null == p && null == q){
return true;
}
if((null == p && null != q) || (null == q && null != p)){
return false;
}
if(p.val == q.val){
return isSameTree(p.left,q.left) ? isSameTree(p.right,q.right) :false;
} else {
return false;
}
}
Please suggest any improvements.