请实现一个函数,用来判断一棵二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
public class Solution {
boolean isSymmetrical(TreeNode pRoot
)
{
if(pRoot
== null
)return true;
return isSame(pRoot
,pRoot
);
}
public boolean isSame(TreeNode pRoot1
,TreeNode pRoot2
){
if(pRoot1
== null
&& pRoot2
== null
)return true;
if(pRoot1
== null
|| pRoot2
== null
)return false;
return pRoot1
.val
== pRoot2
.val
&& isSame(pRoot1
.left
,pRoot2
.right
) && isSame(pRoot2
.right
,pRoot1
.left
);
}
}
转载请注明原文地址:https://tech.qufami.com/read-3272.html