leetcode 对称二叉树 java篇

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

这题的思路也是递归(递归在二叉树中很重要哦)。 

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

 

转载于:https://my.oschina.net/xtzggbmkk/blog/1927492

你可能感兴趣的:(leetcode 对称二叉树 java篇)