【DFS】LeetCode 101. 对称二叉树

题目链接

101. 对称二叉树

思路

DFS 递归解决

代码

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root == null){
            return true;
        }

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

    boolean dfs(TreeNode left, TreeNode right){
        if(left == null && right == null){
            return true;
        }else if((left == null && right != null) || (left != null && right == null)){
            return false;
        }else if(left.val == right.val){
            return dfs(left.left, right.right) && dfs(left.right, right.left);
        }

        return false;
    }
}
posted @ 2023-01-20 10:43  Frodo1124  阅读(25)  评论(0编辑  收藏  举报