题目描述
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
判断两个树是否相等解题思路
继续老传统——递归。
代码
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null){
if(q==null){
return true;
} else {
return false;
}
}
if(q==null){
return false;
}
if(p.val==q.val){
if(isSameTree(p.left,q.left) && isSameTree(p.right,q.right)){
return true;
} else {
return false;
}
} else {
return false;
}
}
}