Unknown's avatar

101. Symmetric Tree

101. Symmetric Tree

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public static boolean isMirror(TreeNode r, TreeNode l){
        if( r == null && l == null) return true;
        if( r == null && l != null) return false;
        if( r != null && l == null) return false;
        if (r.val != l.val) return false;
        if (r.val == l.val) 
        return isMirror(l.right,r.left) && isMirror(l.left, r.right);
         else return false;
    }
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        
        return isMirror(root.right,root.left);
    }
}

Leave a comment