Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created January 6, 2015 06:07
Show Gist options
  • Save kanrourou/472a14a228b00b7a50c1 to your computer and use it in GitHub Desktop.
Save kanrourou/472a14a228b00b7a50c1 to your computer and use it in GitHub Desktop.
/**
* Definition for binary tree
* 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 isSym(root.left, root.right);
}
private boolean isSym(TreeNode left, TreeNode right) {
if (left == null && right == null)
return true;
else if (left != null && right != null) {
if (left.val == right.val)
return isSym(left.left, right.right) && isSym(left.right, right.left);
else
return false;
}
else
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment