解题思路:递归。这道题的难点在于边界条件。确定什么情况下返回什么值。
if(t1 == null & t2 == null)
reutrn true;
if(t1 == null || t2 == null)
return false;
完整代码:
class Solution {
public boolean isSymmetric(TreeNode root) {
return Metric(root,root);
}
public boolean Metric(TreeNode t1,TreeNode t2){
if(t1 == null && t2 == null) return true;
if(t1 == null || t2 == null) return false;
return (t1.val == t2.val) && Metric(t1.left,t2.right) && Metric(t1.right,t2.left);
}
}
来源:CSDN
作者:li1194094543
链接:https://blog.csdn.net/li1194094543/article/details/104146854