Java 1ms recursive


  • 1
    G
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
    	public boolean isSymmetric(TreeNode root) {
            if (root == null) return true;
            return isSymetric(root.left, root.right);
        }
        
        public boolean isSymetric(TreeNode t1, TreeNode t2) {
            if (t1 == null && t2 == null) return true;
            if (t1 == null && t2 != null ||
                t1 != null && t2 == null ||
                t1.val != t2.val)  {
                    return false;
            }
    
        	return isSymetric(t1.left, t2.right) && 
        			isSymetric(t1.right, t2.left);
        }	
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.