LeetCode 101 题解

已经通过

public class LeetCode101 {
    
    public boolean isSymmetric(TreeNode root) {
        if(root == null) return true;
        return isSyme(root.left,root.right);
    }

    /**
     * @param left
     * @param right
     * @return 
     */
    private boolean isSyme(TreeNode left, TreeNode right) {
        if(left == null && right == null) return true;
        if(left !=null && right != null && isEqual(left,right) && isEqual(left.left,right.right) && isEqual(left.right,right.left)){
            return isSyme(left.left,right.right) && isSyme(left.right, right.left);
        }else{
            return false;
        }
    }
    private boolean isEqual(TreeNode node1,TreeNode node2){
        if(node1 == null && node2 == null) return true;
        if(node1 != null && node2 != null && node1.val == node2.val) return true;
        return false;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        
/*       1
        / \
       2   2
      / \ / \
     3  4 4  3*/
        
        /*TreeNode node1 =  new TreeNode(1);
        TreeNode node2 =  new TreeNode(2);
        TreeNode node3 =  new TreeNode(2);
        TreeNode node5 =  new TreeNode(3);
        TreeNode node6 =  new TreeNode(4);
        TreeNode node7 =  new TreeNode(4);
        TreeNode node8 =  new TreeNode(3);
        node1.left = node2;
        node1.right = node3;
        node2.left = node5;
        node2.right = node6;
        node3.left = node7;
        node3.right = node8;*/
        
        TreeNode node1 =  new TreeNode(1);
        TreeNode node2 =  new TreeNode(2);
        TreeNode node3 =  new TreeNode(3);
        node1.left = node2;
        node1.right = node3;
        System.out.println(new  LeetCode101().isSymmetric(node1));
    }

}

wangxiaoming CSDN认证博客专家 架构 Spring Boot Redis
博客是很好的总结和记录工具,如果有问题,来不及回复,关注微信公众号:程序员开发者社区,获取我的联系方式,向我提问,也可以给我发送邮件,联系 1275801617@qq.com
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页
实付 19.89元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值