Title description
Given a binary tree, judge whether Qi is a mirror image of itself (that is, whether it is symmetrical).
For example: the following binary tree is symmetric
1
/ \
2 2
/ \ / \
3 4 4 3
The following binary tree is asymmetric.
1
/ \
2 2
\ \
3 3
Note:
I hope you can use recursion and iteration to solve this problem
Example 1
enter
{1,2,2}
return value
true
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param root TreeNode类
* @return bool布尔型
*/
public boolean isSymmetric (TreeNode root) {
return preTree(root,root);
}
public boolean preTree(TreeNode tree1,TreeNode tree2){
if(tree1==null && tree2==null){
return true;
}
if(tree1==null || tree2==null){
return false;
}
if(tree1.val != tree2.val){
return false;
}
return preTree(tree1.left,tree2.right) && preTree(tree1.right,tree2.left);
}
}