<> Sword finger Offer 28. Symmetric binary trees

Please implement a function , Used to determine whether a binary tree is symmetric . If a binary tree is the same as its mirror image , So it's symmetrical .

for example , Binary tree [1,2,2,3,4,4,3] It's symmetrical .
1 / \ 2 2 / \ / \ 3 4 4 3
But here's the next one [1,2,2,null,3,null,3] It's not mirror symmetric :
1 / \ 2 2 \ \ 3 3
<> Examples 1:

input :root = [1,2,2,3,4,4,3]
output :true

<> Examples 2:

input :root = [1,2,2,null,3,null,3]
output :false

<> Problem solving
package tree. Mirror image of binary tree ; import java.util.ArrayList; import java.util.Arrays;
import java.util.LinkedList; import java.util.List; /** * @Auther: truedei *
@Date: 2020 /20-6-10 * @Description: Binary tree image */ public class Solution { static
public boolean isSymmetric(TreeNode root) { // The root node is null Time , return true, The explanation is return root
== null?true:check(root.left,root.right); } private static boolean check(
TreeNode left, TreeNode right) { if(left == null && right==null) return true; if
(left==null || right==null || left.val != right.val) return false; return check(
left.left,right.right) && check(left.right,right.left); } public static void
main(String[] args) { TreeNode t1 = new TreeNode(1); TreeNode t2 = new TreeNode(
2); TreeNode t3 = new TreeNode(2); TreeNode t4 = new TreeNode(3); TreeNode t5 =
new TreeNode(4); TreeNode t6 = new TreeNode(4); TreeNode t7 = new TreeNode(3);
t1.left=t2; t1.right=t3; t2.left=t4; t2.right=t5; t3.left=t6; t3.right=t7;
boolean bool = isSymmetric(t1); System.out.println(bool); } } class TreeNode {
int val;// Data stored in each node TreeNode left;// Left node TreeNode right;// Right node TreeNode(int x) {
val= x; } }
Core code :
static public boolean isSymmetric(TreeNode root) {
// If it's coming from null, Return to true, Otherwise, check the left and right nodes return root == null?true:check(root.left,root.
right); } // Check the left and right nodes recursively private static boolean check(TreeNode left, TreeNode
right) { // If both sides are null, It shows that the binary tree is also symmetric if(left == null && right==null) return true;
// If one of the left and right child nodes is missing , Or not equal , It's not a symmetric binary tree if(left==null || right==null || left.val !=
right.val) return false; // Otherwise check , The left node of the binary tree is the left node and the right node is the right node ; And check the other two // If it's all true, Explain symmetry return
check(left.left,right.right) && check(left.right,right.left); }

Technology
©2019-2020 Toolsou All rights reserved,
Final review of database : Summary of comprehensive application questions use Python Make simple games Laplance operator ( Second derivative ) Convert hard disk to GPT Partition format Python Implementation of Hanoi Tower code about String How to create objects vue3 Learning journey 1—— establish vue3 project java String from back to front _Java String String summary use Python Write a story about plants versus zombies 【 Greedy Algorithm 】 Huffman coding problem