Created
February 13, 2014 16:27
-
-
Save b27lu/8978452 to your computer and use it in GitHub Desktop.
Symmetric Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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