2018-08-10 剑指offer 对称的二叉树

题目
/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    boolean isSymmetrical(TreeNode pRoot)
    {
        if(pRoot == null) return true; //判断为空
        return comparenode(pRoot.left,pRoot.right);
    }
    
    private boolean comparenode(TreeNode left,TreeNode right){
            if(left == null && right == null){ // 结束
                return true;
            }
            if(left !=null && right != null){
                if(left.val == right.val){
                    if(comparenode(left.left,right.right)&&comparenode(left.right,right.left)){ // 同时比较两端
                        return true;    
                    }
                }
            }
        
            return false;
    }
}

你可能感兴趣的:(2018-08-10 剑指offer 对称的二叉树)