LeetCode 101 symmetric tree

Checking if a given tree is symmetric tree or not.

so all we need to do is write the recursion equations:
isSymmetric(root) = isSym(root.left, root.right) //two sub tree is sym
isSym(left, right) = (left.val == right.val) && isSym(left.left, right.right) && isSym(left.right, right.left);

就跟LC100类似 很简单

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

posted @ 2020-11-13 02:03  EvanMeetTheWorld  阅读(21)  评论(0)    收藏  举报