Advertisement
Guest User

Symmetric Tree

a guest
Sep 21st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
  2.  
  3. For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
  4.  
  5. 1
  6. / \
  7. 2 2
  8. / \ / \
  9. 3 4 4 3
  10. But the following [1,2,2,null,3,null,3] is not:
  11. 1
  12. / \
  13. 2 2
  14. \ \
  15. 3 3
  16.  
  17. //////////////////////////
  18.  
  19. class Solution {
  20. public boolean isSymmetric(TreeNode root) {
  21. if(root == null)
  22. return true;
  23.  
  24. return checkSame(root.left,root.right);
  25. }
  26.  
  27. public boolean checkSame(TreeNode left, TreeNode right) {
  28. if(left == null && right == null)
  29. return true;
  30. if(left == null || right == null)
  31. return false;
  32. if(left.val == right.val)
  33. return checkSame(left.left, right.right) && checkSame(left.right,right.left);
  34. return false;
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement