首页 > 其他分享 >【DFS】LeetCode 101. 对称二叉树

【DFS】LeetCode 101. 对称二叉树

时间:2023-01-20 11:02:50浏览次数:57  
标签:101 return dfs right 二叉树 && null LeetCode left

题目链接

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;
    }
}

标签:101,return,dfs,right,二叉树,&&,null,LeetCode,left
From: https://www.cnblogs.com/shixuanliu/p/17062520.html

相关文章