一颗树是不是轴对称

https://oj.leetcode.com/problems/symmetric-tree/


/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null) return true;
if(root.left==null&&root.right==null)
{
return true;
}

if(root.left!=null&&root.right!=null)
{

return is(root.left,root.right);
}

return false;



}


public boolean is(TreeNode l,TreeNode r)
{
if(l==null&&r==null) return true;
if(l==null||r==null) return false;
//now th l and r are not null
if(l.val!=r.val) return false;
//now l.val==r.val

return is(l.right,r.left)&&is(l.left,r.right);


}
}

你可能感兴趣的:(树)