Why is the general solution, learn BFC this topic really easy to win in 10 minutes, no suspense. If you are not familiar with BFC, check out this article on documented BFS issues

The title

LeetCode 101 Leetcode-cn.com/problems/sy…

Analysis of the

The BFS solution uses a queue to cache all nodes of the current layer, and compares the positive order and reverse order to see if they are equal. If the middle is not equal, return directly. And then you keep iterating until you’ve traversed all the nodes.

implementation

BFS traverses each level of the binary tree and compares whether the node values at that level are mirrored. If you don’t know how to traverse a binary tree via BFC

/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; *} * /
/ * * *@param {TreeNode} root
 * @return {boolean}* /
var isSymmetric = function(root) {
    // Recursive end condition
    // 1. There are no child nodes, and the preceding nodes are stacked
    // 2. There are child nodes, but there is an asymmetry
    if(! root)return true;
    let list = [root];
    let flag = true;

    const dp = (list) = > {
        let tmp = [...list].map(item= >item ? (item.val ! =null ? item.val :'null') : 'null');
        if(tmp.join(' ') != tmp.reverse().join(' ')) {
            flag = false
            return;
        }

        while(list.length) {
            let len = list.length;
            for(let i = 0; i < len; i++) {
                const node = list.shift();
                if(node) {
                    list.push(node.left);
                    list.push(node.right);
                }
            }
            dp(list)
            
        }   
    }

    dp(list);

    return flag;
};
Copy the code

“A front-end learning small transparent, hard to learn front-end knowledge, at the same time to share their own thoughts on life, welcome to discuss and exchange. If my article is helpful to you, please click a like, will be very grateful for your encouragement.”

Other articles on Algorithms:

Dynamic programming smart change problem

Documented BFS problems

Backtracking algorithm solves the complete permutation problem

Landing the general solutions of symmetric binary tree | creator training camp The campaign is under way…