The title

LeetCode 第 101, Symmetric binary tree association type: tree

Given a binary tree, check whether it is mirror - symmetric. For example, binary trees [1,2,2,3,4,4,3] are symmetric. 1 / \ 2 2 / \ / 3 4 4 3 But the following [1,2,2,null,3,null,3] is not mirror symmetric: 1 / \ 2 2\3 3 progression: can you use both recursion and iteration to solve this problem?Copy the code

Time to solve the problem.

/**
 * 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) {
       
       
       
    }
}

Copy the code

The method input parameters are given above to complete the answer.

Subject analysis

  1. This problem is very similar to the same tree problem, and you can do it with the same idea
  2. Make judgments about situations
  3. Recursively call the method to complete the solution

Answers to analysis

This article only analysis I do the idea, only for reference, to understand a solution to the idea, other kinds of ideas to do the problem please access the Internet.

Answer successful: Execution time :0 ms, beat 100.00% Java user memory consumption :36.3 MB, beat 81% of Java users

class Solution { public boolean isSymmetric(TreeNode root) { if (root ! = null) { return isSame(root.left, root.right); } else {// return false for null; } } public boolean isSame(TreeNode left, TreeNode right) { if (left ! = null && right ! = null) {if (left. Val == right.val) {return isSame(left. Left, right. right.left); } else {return false; }} else if (left == null &&right == null) {return true; } else {// there is a null return false; }}}Copy the code