[Employment front-end evil compensation algorithm]JavaScript leetcode top100 (2): Symmetric binary tree, maximum depth of binary tree, best time to buy and sell tickets, numbers that only appear once, circular linked list

Column statement: Just use the simplest and easy-to-understand method to pass, don't seek optimization, don't spray if you don't like it,
update five easy difficulty questions today:

Symmetric Binary Tree

  • Topic
    Given you the root node root of a binary tree, check whether it is axisymmetric.
  • Knowledge point:
    binary tree
  • The idea is
    to traverse the binary tree from top to bottom, and pass in two symmetrical nodes each time. If they are different, return asymmetrical. If they are the same, then traverse the left child of the left node and the right child of the right node, and then traverse the left node The right child and the left child of the right node, until the traversed node is null
  • the code
var isSymmetric = function (root) {
    
    
    let f = true;
    let dfs = function (node, node2) {
    
    
        if (node == null && node2 == null) {
    
    
            return;
        }
        if (node == null || node2 == null || node.val !== node2.val) {
    
    
            f = false;
            return;
        }
        dfs(node.left, node2.right);
        dfs(node.right, node2.left);
        return;
    }
    dfs(root.left, root.right);
    return f;
};

The maximum depth of the binary tree

  • 题目:
    Given a binary tree, find its maximum depth.
    The depth of a binary tree is the number of nodes on the longest path from the root node to the furthest leaf node.
  • Knowledge point
    binary tree, depth-first search
  • Idea
    Traverse the binary tree from top to bottom, carry the current height, and increase the height by 1 when traversing child nodes. If the current height is greater than the highest height, update the highest height.
  • the code
var maxDepth = function (root) {
    
    
    let max = 0;
    let dfs = (root, h) => {
    
    
        if(!root){
    
    
            return ;
        }
        if (h > max) {
    
    
            max = h;
        }
        if (root.left) {
    
    
            dfs(root.left, h + 1)
        }
        if (root.right) {
    
    
            dfs(root.right, h + 1)
        }
    }
    dfs(root, 1)
    return max;
};

The best time to buy and sell stocks

  • Topic
    Given an array prices, its i-th element prices[i] represents the price of a given stock on the i-th day.
    You can only choose to buy the stock on one day and sell it on a different day in the future. Design an algorithm to calculate the maximum profit you can make. Returns the maximum profit you can make from this trade. If you can't make any profit, return 0.
  • Dynamic programming of knowledge points
  • Idea
    DP idea, for each bit, update it to the minimum value of the stock price up to the current position. Traverse the entire array sequentially, each time first calculate whether the current price minus the previous minimum value is greater than the maximum return, if so, update the maximum return, and then update the current position to the minimum value of the first n stock prices.
  • the code
var maxProfit = function (prices) {
    
    
    let max = 0;
    for (var i = 1; i < prices.length; i++) {
    
     
        if (prices[i] - prices[i - 1] > max) {
    
    
            max = prices[i] - prices[i - 1];
        }
        prices[i] = Math.min(prices[i - 1], prices[i]);
    }
    return max;
};

number that occurs only once

  • Topic
    Given a non-empty integer array nums, each element appears twice except for a certain element that appears only once. Find the element that appears only once.
    You must design and implement an algorithm of linear time complexity to solve this problem that uses only constant extra space.
  • Knowledge point
    mathematics
  • Ideas
    Know that a number is XORed with itself and the result is 0, so we OR all the data in the entire array, and we get the number we need
  • the code
var singleNumber = function (nums) {
    
    

    let a = 0;
    for (var i = 0; i < nums.length; i++) {
    
    
        a = a ^ nums[i];
    }
    return a;

};

circular linked list

  • Topic
    Given you a head node head of a linked list, determine whether there is a ring in the linked list.
    If there is a node in the linked list that can be reached again by continuously tracking the next pointer, then there is a cycle in the linked list. In order to represent the ring in the given linked list, the evaluation system internally uses the integer pos to indicate the position where the end of the linked list is connected to the linked list (the index starts from 0). Note: pos is not passed as a parameter. Just to identify the actual situation of the linked list.
    Returns true if there is a cycle in the linked list. Otherwise, returns false.

  • Linked list of knowledge points

  • Idea
    Define two pointers, one takes one step at a time, and the other takes two steps at a time. If the fast pointer reaches the bottom of the linked list, then the linked list has no ring, otherwise the two pointers will eventually meet at a certain node

  • the code

var hasCycle = function (head) {
    
    
    let a = head;
    let b = head;
    while (a) {
    
    
        a = a.next;
        if (a) {
    
    
            a = a.next;
        }else{
    
    
            return false;
        }
        b = b.next;
        if(a == b){
    
    
            return  true;
        }
    }
    return false;
};

Guess you like

Origin blog.csdn.net/weixin_46463785/article/details/129274178