Skip to content

Instantly share code, notes, and snippets.

@b27lu
Created February 13, 2014 16:27
Show Gist options
  • Save b27lu/8978452 to your computer and use it in GitHub Desktop.
Save b27lu/8978452 to your computer and use it in GitHub Desktop.
Symmetric Tree
/**
* 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 dfsWorker(root.left, root.right);
}
public boolean dfsWorker(TreeNode left, TreeNode right){
if(left == null && right == null)
return true;
boolean result = false;
if(left != null && right != null && left.val == right.val)
result = dfsWorker(left.left, right.right) && dfsWorker(left.right, right.left);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment