A, the title

// Given a binary tree, check if it is mirror - symmetric. // // // for example, binary trees [1,2,2,3,4,4,3] are symmetric. / / / / / / / / / 1/2 / / / \ \ / 2/3 3/4 4 / / / / / but this [1,2,2, null, 3, null, 3] are not mirror symmetry: // // 1 // / \ // 2 2 // \ // 3 3Copy the code

2. How to solve the problem

Split the mirror tree into two binary trees, return true if all mirror conditions are met, false if not, recursive comparison from top to bottom

Three, code,

/** * 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) { return isMirrorImageTree(root,root); } public boolean isMirrorImageTree(TreeNode root1,TreeNode root2){ if (null == root1 && null == root2){ return true; } if (null == root1 || null == root2){ return false; } // Divide the tree into two binary trees, if all conditions are met, return true, if not, return false, recursive comparison from top down //1. (1) The root of the two trees is equal. (2) The child on the left of tree 1 equals the child on the right of tree 2. (3) The child on the right of tree 1 equals the child on the left of tree 2. Val == root2.val && isMirrorImageTree(root1.left,root2.right) && isMirrorImageTree(root1.right,root2.left); } } //leetcode submit region end(Prohibit modification and deletion)Copy the code