图源:新生大学
https://leetcode.com/problems/symmetric-tree/
Input: So, we're given a binary tree.
**Output: ** If it's symmetric, we will return True, otherwise False.
**Corner case: ** If the tree is null, the output is True.
**Core case: **
The primitive idea is that, we may use two Deque on each level.
Or, we can apply a helper function to optimize this process.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root){
//corner case
if ( root == null )
return true;
//core logic
return isSymmetric(root.left, root.right);
}
//helper function
public boolean isSymmetric(TreeNode one, TreeNode two){
//base case
if( one == null || two == null ){
if ( one == two )
return true;
else
return false;
}
//current layer
if ( one.val != two.val )
return false;
//next layer
else
return isSymmetric( one.left, two.right ) && isSymmetric( one.right, two.left );
}
}