2

This topic aims to share some interesting or valuable topics found in the process of brushing Leecode. [Of course, the answer is based on js].

Recursive algorithms have always been one of the key types of leetcode's moderately difficult problems, so the criticality is self-evident.

topic related

    1
   / \
  2   2
 / \ / \
3  4 4  3

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

    1
   / \
  2   2
   \   \
   3    3

Tips

Considering that some students may seldom use js to brush leetcode, we will briefly introduce the data input tree type As in the above question, the input of the given binary tree , but should be as follows: Each node is a object :

 const root = {
      val: 3,
      left: { // left表示当前节点的左侧子节点
        val: 9,
        left: null, // 对照上图可以看到 节点9没有左右子节点
        right: null,
      },
      right: {
        val: 20,
        left: {
          val: 15, // 对照上图可以看到 节点15没有左右子节点
          left: null, 
          right: null,
        }, 
        right: {
          val: 7, // 对照上图可以看到 节点7没有左右子节点
          left: null, 
          right: null,
        },
      }
    }

Analysis of ideas

First of all, this question is an obvious recursive class problem, and the recursive class problem is generally the following steps:

  1. Logic to extract recursive part
  2. Judging boundary conditions

image.png

  • First of all, in terms of overall logic, to determine that a tree is a mirrored binary tree, it must be starting from the left and right nodes of the root node and . Each group of nodes (represented by L and R) encountered during the traversal process are all To satisfy L's left node = R's right node and L's right node = R's left node because recursively compares , so here equal to as long as the val is equal:
    image.png
  • The second is to consider the boundary condition

    1. If it is a empty tree , then return the result directly, which is also considered symmetrical;
    2. During synchronous comparison, at any step, as long as there is L = R's right node and L's right node = R's left node , it will end in advance and return false;
    3. If the comparison ends at the same time on both sides, it means that it is a symmetric tree , and returns true;

Well, according to previously said, the first to write recursive part logic:

    var recuCompare = function (L, R) {
        if(!L && !R) { // 说明同时比对同时结束 或者是两边均无该子节点
            return true;
        }
        if(!L || !R || L.val !== R.val ) { // 扣除第一种情况 那么!L 或者 !R说明左右不同时结束,也就是出现不对称
            return false;
        }
        return recuCompare(L.left, R.right) && recuCompare(L.right, R.left); // 继续往下比对
    }

Then fill in the recursive start condition part, and the complete code can be obtained.

full code

/**
 * 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) {
    if(!root) {return true};
    return recuCompare(root.left, root.right)
};
var recuCompare = function (L, R) {
    if(!L && !R) {
        return true;
    }
    if(!L || !R || L.val !== R.val )  {
        return false;
    }
    return recuCompare(L.left, R.right) && recuCompare(L.right, R.left);
}

have you found that recursive questions are all codes that are not long after they are written, but it is difficult to understand , so you need to try to focus on the logic.

I highly recommend that after reading it, write a code by yourself and run to see
I highly recommend that after reading it, write a code by yourself and run it to see
I highly recommend that you write a code by yourself after reading it and run to see

Because the writing and details of many boundary conditions are actually the most common bugs in the debugging process.

In addition, you can use the ideas of this question to go to try to solve this recursive backtracking question - the substructure of the tree to consolidate what you have learned https://leetcode-cn.com/problems/shu-de-zi -jie-gou-lcof/

Then, the simple question is done again!

image.png


安歌
7k 声望5.5k 粉丝

目前就职于Ringcentral厦门,随缘答题, 佛系写文章,欢迎私信探讨.