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 🦀
111. Minimum depth of a binary tree
Difficulty is simple
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node to the nearest leaf node.
Note LEAF nodes refer to nodes that have no child nodes.
Example 1:
Input: root = [3,9,20,null,null,15,7] output: 2Copy the code
Example 2:
Input: root = [2, null, 3, null, 4, null, 5, null, 6] output: 5Copy the code
Tip:
- The number of nodes in the tree ranges from
[0, 105]
内 -1000 <= Node.val <= 1000
🌵
- To find the minimum depth, consider using breadth-first traversal.
- In breadth-first traversal, determine whether each node is a leaf node. If so, stop traversal and return to node level.
How to solve the problem 🐂
-
Breadth-first traverses the entire tree and records the hierarchy of each node.
-
When a leaf node is encountered, return to the node level and stop traversing.
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
* @return {number}* /
var minDepth = function(root) {
if(! root){return 0; }const que=[[root,1]].while(que.length){
const [n, l]=que.shift();
if(! n.left && ! n.right){return l;
}
if(n.left)que.push([n.left,l+1])
if(n.right)que.push([n.right,l+1])}};Copy the code
Time complexity :O(n) (n is the number of nodes in the tree)
Space complexity :O(n)
Conclusion 🌞
So the LeetCode 111- binary tree minimum depth of Yu Yu LeetCode algorithm is over, there is no shortcut to the algorithm, can only write more practice, more summary, the purpose of the article is actually very simple, is to urge myself to complete the 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 interesting front-end, know I am not bad 🌟 ~)