LeeCode (recursion, queue) 101_ symmetric binary tree

LeeCode (recursion, queue) 101_ symmetric binary tree

Problem:
Given a binary tree, check whether it is mirror-symmetrical.

For example, the binary tree [1,2,2,3,4,4,3] is symmetric.

1

/
2 2
/ \ /
3 4 4 3

But the following [1,2,2,null,3,null,3] is not mirror symmetry:

1

/
2 2
\
3 3

Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/symmetric-tree
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
Problem-solving ideas:

Method one:
recursion

  1. If the left subtree and the right subtree of a tree are mirrored and symmetrical, then the tree is symmetrical.
  2. Therefore, the question can be transformed into: Under what circumstances are the two trees mirror images of each other?
    If the following conditions are met at the same time, the two trees are mirror images of each other:
  3. Their two root nodes have the same value
  4. The right subtree of each tree is mirrored and symmetrical with the left subtree of the other tree

Java code:

import java.util.LinkedList;
import java.util.Queue;

class TreeNode {
    
    
	  int val;
	  TreeNode left;
	  TreeNode right;
	  TreeNode(int x) {
    
     val = x; }
 }


public class 对称二叉树 {
    
    
	public boolean isSymmetric(TreeNode root) {
    
    
        return check(root, root);
    }

	private boolean check(TreeNode p, TreeNode q) {
    
    
		if(p==null && q==null){
    
    
			return true;
		}
		if(p==null || q==null){
    
    
			return false;
		}
		return p.val==q.val && check(p.left,q.right) && check(p.right,q.left);
	}
}


Method 2:
Queue
We put the root node into the queue twice during initialization. Extract two nodes at a time and compare their values ​​(every two consecutive nodes in the queue should be equal, and their subtrees are mirror images of each other), and then reverse the left and right sub-nodes of the two nodes Insert the order into the queue. When the queue is empty, or we detect that the tree is asymmetric (that is, when two consecutive unequal nodes are removed from the queue), the algorithm ends.

Java code:

import java.util.LinkedList;
import java.util.Queue;

class TreeNode {
    
    
	  int val;
	  TreeNode left;
	  TreeNode right;
	  TreeNode(int x) {
    
     val = x; }
 }


public class 对称二叉树 {
    
    
	public boolean isSymmetric(TreeNode root) {
    
    
        return check(root, root);
    }
	
	public boolean check(TreeNode u, TreeNode v) {
    
    
		Queue <TreeNode> q = new LinkedList<TreeNode>();
		q.offer(u);
		q.offer(v);
		
		while(!q.isEmpty()){
    
    
			u = q.poll();
			v = q.poll();
			
			if(u == null && v == null){
    
    
				continue;
			}
			
			if((u == null || v == null) || (u.val != v.val)){
    
    
				return false;
			}
			
			q.offer(u.left);
			q.offer(v.right);
			
			q.offer(u.right);
			q.offer(v.left);
		}
		return true;
	}
}

Guess you like

Origin blog.csdn.net/u013456390/article/details/112011026