Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode() {}
- * TreeNode(int val) { this.val = val; }
- * TreeNode(int val, TreeNode left, TreeNode right) {
- * this.val = val;
- * this.left = left;
- * this.right = right;
- * }
- * }
- */
- class Solution {
- public boolean isSymmetric(TreeNode root) {
- if (root == null) {
- return true;
- }
- return mirror(root.left,root.right); // check the tree
- // 1
- } // / \
- // 2 2
- // /
- // 3 4 4 3
- public boolean mirror(TreeNode l, TreeNode r){ // l.l == r.r
- // l.r == r.l
- if(l==null&&r==null){ // symmetric
- return true;
- }else if(l!=null&&r!=null){
- return l.val==r.val && mirror(l.left,r.right)&& //
- mirror(l.right,r.left);
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement