Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money

Hello everyone, I am quick-frozen fish 🐟, a front end of water system 💦, like colorful whistle 💐, continuous sand sculpture 🌲, I am a good brother of the next door Cold grass Whale, I just started to write an article. If you like my article, you can follow ➕ to like it, inject energy into me, and grow with me

Preface 🌧 ️

Algorithms are unfamiliar and familiar to the front-end people, and often we don’t value them as much as the back-end engineers do. But in fact, algorithms have an unshakable position for every programmer.

Because the development process is to convert the actual problem into the computer can recognize the instructions, that is, “data structure” said, “design the data structure, in the application of algorithms on the line”.

The quality of writing instructions will directly affect the performance of the program, and instructions are composed of data structure and algorithm, so the design of data structure and algorithm basically determines the quality of the final program.

In addition, when reading the source code, the lack of understanding of algorithms and data structures makes it difficult to understand why the author wrote the way he did.

In today’s environment, algorithms have become an indispensable skill for front-end engineers. If we want to move beyond being application engineers writing business code, we need to understand algorithms and data structures.

Of course, learning is also focused, as the front end we do not need to fully grasp the algorithm like back-end development, some of the more partial, not practical type and solution method, as long as a little understanding.

The title 🦀

112. Sum of paths

Difficulty is simple

You are given the root node of the binary tree and an integer targetSum representing the targetSum. Determine if there is a path from the root node to the leaf node in the tree. The sum of all nodes in the path equals the target and targetSum.

A leaf node is a node that has no children.

Example 1:

Input: root = [13,4,7,2 5,4,8,11, null, null, null, null, 1], targetSum = 22 output: trueCopy the code

Example 2:

Input: root = [1,2,3], targetSum = 5 output: falseCopy the code

Example 3:

Input: root = [1,2], targetSum = 0 output: falseCopy the code

Tip:

  • The number of nodes in the tree is in the range[0, 5000]
  • -1000 <= Node.val <= 1000
  • -1000 <= targetSum <= 1000

🌵

  • During depth-first traversal, the sum of the node values of the current path is recorded.
  • At the leaf node, determine whether the sum of the node values of the current path equals the target value

How to solve the problem 🐂

  • Depth traversal binary tree, at the leaf node, judge whether the sum of the node values of the current path is equal to the target value, if yes, return

  • If there is no match, go back to false.

Source 🔥

/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */
/ * * *@param {TreeNode} root
 * @param {number} targetSum
 * @return {boolean}* /
var hasPathSum = function(root, targetSum) {
        if(! root)return false
        let res=false;
        const dfs=(n,s) = >{
            if(! n)return 
            console.log(n.val,s)
            if(! n.left&&! n.right&&s===targetSum){res=true}
            if(n.left)dfs(n.left,s+n.left.val)
            if(n.right)dfs(n.right,s+n.right.val)
    }
    dfs(root,root.val)
    return res
};
Copy the code

Time complexity :O(n) (n is the number of nodes in the tree)

Space complexity :O(n)/Olog(n)

Conclusion 🌞

So yu Yu’s LeetCode algorithm “LeetCode 112- path sum” is over, there is no shortcut to algorithm, can only write more practice, more summary, the purpose of the article is actually very simple, is to urge myself to complete algorithm practice and summary and output, dishes are not important, but love 🔥, I hope everyone can like my essay, and I also hope to know more like-minded friends through the article. If you also like to toss, welcome to add my friend, sand sculpture together, together progress.

Making 🤖 : sudongyu

Personal blog 👨💻: Frozen fish blog

Vx 👦 : sudongyuer

Write in the last

Guys, if you like my words, give 🐟🐟 a thumbs up 👍 or follow ➕ to support me the most.

Add me on wechat: Sudongyuer, invite you into the group and learning the front together, become a better engineer ~ (group of qr code here – > front to go to bed early, qr code has expired, see the links to the boiling point in the comments, I will put the latest qr code in the comments section, of course, can also add me WeChat I pull you into the group, after all, I also am interesting front end, I also not bad 🌟 ~

“Welcome to the discussion in the comments section. The nuggets will be giving away 100 nuggets in the comments section after the diggnation project. See the event article for details.”