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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s