判断两个二叉树是否相同,包括结构和数值
直接递归遍历即可
/**
* 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 p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
else if(p == null && q == null){
return true;
}else{
return false;
}
}
}