题目:
解题思路:可以新写一个函数,从root开始,root的left的头结点将记为lefttree(左子树),root的lright的头结点将记为righttree(右子树),
然后递归左子树的root.left与右子树的root.right,左子树的root.right与右子树的root.left进行比较,
一共有四种情况,第一种左子树与右子树都为空,第二种左子树或者右子树为空,第三种lefttree与righttree的值不一样,第四种则是左子树与右子树对称
解题代码:
class Solution {public boolean isSymmetric(TreeNode root) {if(root==null){return true;}return isequal(root.left,root.right);}public boolean isequal(TreeNode treeleft,TreeNode treeright){if(treeleft==null&&treeright==null){return true;}if(treeleft==null||treeright==null){return false;}if(treeleft.val!=treeright.val){return false;}return isequal(treeleft.left,treeright.right)&& isequal(treeleft.right,treeright.left);}
}