26. Symmetric Binary Trees (159)
-
Title Description: Please implement a function to determine whether a binary tree is symmetrical. Note that if a binary tree has the same mirror image as the binary tree, it is defined as symmetric.
-
Train of thought:
- Method 1: Recursion
- Method 2: Using stack, symmetrical nodes are continuously stacked, and then out of the stack to determine whether they are symmetrical or not.
- Method 3: Using the team, the symmetrical nodes will be continuously entered into the team, and then out of the team to determine whether symmetrical.
-
Code:
package _26.Symmetric Binary Trees; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /** * Title Description: Please implement a function to determine whether a binary tree is symmetrical. Note that if a binary tree has the same mirror image as the binary tree, it is defined as symmetric. * * @author Administrator * */ public class SymmetricalBinaryTree { /** * Method 1: Recursion * @param pRoot * @return */ static boolean isSymmetrical(TreeNode pRoot) { if (pRoot == null) return true; return isSymmetrical(pRoot.left, pRoot.right); } static boolean isSymmetrical(TreeNode root1, TreeNode root2) { // Nodes have been compared if (root1 == null && root2 == null) return true; // Node inconsistency if (root1 == null || root2 == null) return false; // Node values are inconsistent if (root1.val != root2.val) return false; // Node values are equal, continue to compare return isSymmetrical(root1.left, root2.right) && isSymmetrical(root1.right, root2.left); } /** * Method 2: Use stack to stack symmetrical nodes, and then compare whether the two nodes are identical. * @param pRoot * @return */ static boolean isSymmetrical1(TreeNode pRoot) { if (pRoot == null) return true; Stack<TreeNode> stack = new Stack<>(); TreeNode left = pRoot.left; TreeNode right = pRoot.right; stack.push(right); stack.push(left); while(!stack.isEmpty()){ left = stack.pop(); right = stack.pop(); //Compare the values of symmetric nodes if(left == null && right == null) continue; if(left == null || right == null) return false; if(left.val != right.val) return false; stack.push(left.left); stack.push(right.right); stack.push(left.right); stack.push(right.left); } return true; } /** * Method 3: Use queues to stack symmetrical nodes, and then compare whether the two nodes are identical. * @param pRoot * @return */ static boolean isSymmetrical2(TreeNode pRoot) { if(pRoot == null) return true; Queue<TreeNode> queue = new LinkedList<>(); TreeNode left = pRoot.left; TreeNode right = pRoot.right; queue.add(left); queue.add(right); while(!queue.isEmpty()){ left = queue.remove(); right = queue.remove(); if(left == null && right == null) continue; if(left == null || right == null) return false; if(left.val != right.val) return false; queue.add(left.left); queue.add(right.right); queue.add(left.right); queue.add(right.left); } return true; } public static void main(String[] args) { TreeNode root = new TreeNode(8); root.left = new TreeNode(6); root.right = new TreeNode(6); root.left.left = new TreeNode(5); root.left.right = new TreeNode(7); root.right.left = new TreeNode(7); root.right.right = new TreeNode(5); System.out.println(isSymmetrical2(root)); // System.out.println(isSymmetrical2(root)); } } class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } }