leetcode -- 101. Symmetric Tree

题目描述

题目难度:Easy

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
在这里插入图片描述

AC代码

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root == null) return true;
        return isSymmetric(root.left, root.right);
    }
    
    private boolean isSymmetric(TreeNode p, TreeNode q){
        if(p == null && q == null) return true;
        if(p == null || q == null) return false;
        if(p.val == q.val) return isSymmetric(p.left, q.right) && isSymmetric(p.right, q.left);
        return false;
    }
}

猜你喜欢

转载自blog.csdn.net/tkzc_csk/article/details/88663134