From 8a5e9151ae4207668ecb2afde3f0a98ffb9a09c7 Mon Sep 17 00:00:00 2001 From: Aysia Brown Date: Sun, 7 Feb 2021 14:46:54 -0800 Subject: [PATCH 1/2] min depth of binary tree --- .../111-minimum-depth-of-binary-tree.js | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 JavaScript/111-minimum-depth-of-binary-tree.js diff --git a/JavaScript/111-minimum-depth-of-binary-tree.js b/JavaScript/111-minimum-depth-of-binary-tree.js new file mode 100644 index 00000000..86f76893 --- /dev/null +++ b/JavaScript/111-minimum-depth-of-binary-tree.js @@ -0,0 +1,49 @@ +/** + * 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} + */ + + // test cases: + // [3,9,20,null,null,15,7] -- output: 2 +// [2,null,3,null,4,null,5,null,6] -- output: 5 +// [0] -- output: 1 +// [] -- output: 0 + + +var minDepth = function(root) { + if(root) { + return traversal(root, 1); + } else { return 0 } +}; + +var traversal = (root, depth) => { + let current = root; + let right; + let left; + if(!current) { + return null; + } if (!current.right && !current.left) { + return depth; + } + right = traversal(current.right, depth+1) + left = traversal(current.left, depth+1) + if(!right) { + return left; + } else if(!left) { + return right; + } else { + if(right < left) { + return right + } else { + return left + } + } +} \ No newline at end of file From 2a364d3cc072f92000410068ec8ddd84d5a3a91e Mon Sep 17 00:00:00 2001 From: Aysia Brown Date: Sun, 7 Feb 2021 14:57:06 -0800 Subject: [PATCH 2/2] added comments, updated readme --- JavaScript/111-minimum-depth-of-binary-tree.js | 7 +++++++ README.md | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/JavaScript/111-minimum-depth-of-binary-tree.js b/JavaScript/111-minimum-depth-of-binary-tree.js index 86f76893..244990cd 100644 --- a/JavaScript/111-minimum-depth-of-binary-tree.js +++ b/JavaScript/111-minimum-depth-of-binary-tree.js @@ -11,6 +11,10 @@ * @return {number} */ +// PROBLEM: +// Given a binary tree, find its minimum depth. +// The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. + // test cases: // [3,9,20,null,null,15,7] -- output: 2 // [2,null,3,null,4,null,5,null,6] -- output: 5 @@ -24,12 +28,15 @@ var minDepth = function(root) { } else { return 0 } }; +// traversal of the tree is recursive to ensure travel down +// both the right and left children nodes var traversal = (root, depth) => { let current = root; let right; let left; if(!current) { return null; + // this checks if the current node is a leaf node } if (!current.right && !current.left) { return depth; } diff --git a/README.md b/README.md index f53ccb34..93cbe376 100644 --- a/README.md +++ b/README.md @@ -320,8 +320,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 127 | [Word Ladder](https://leetcode.com/problems/word-ladder/) | [Java](./Java/word-ladder.java) | O(N^2 \* M) | O(N \* M) | Medium | BFS | | 994 | [Rotten Oranges](https://leetcode.com/problems/rotting-oranges/) | [Python](./Python/994_Rotting_Oranges.py) | O(N \* M) | O(N \* M) | Medium | BFS | | 743 | [Network Delay Time](https://leetcode.com/problems/network-delay-time/) | [C++](./C++/Network-delay-time.cpp) | _O(V+E))_ | O(V) | Medium | BFS | - - + 111 | [Min Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [JavaScript](./JavaScript/111-minimum-depth-of-binary-tree.js) | O(nlogn) | O(nlogn) | Easy | BFS
⬆️ Back to Top @@ -481,7 +480,8 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if | [Franchis N. Saikia](https://github.com/Francode007)
| India | C++ | [Github](https://github.com/Francode007) | | [Yarncha](https://github.com/yarncha)
| South Korea | C++ | [LeetCode](https://leetcode.com/yamcha/) | | [Gamez0](https://github.com/Gamez0)
| South Korea | Python | [LeetCode](https://leetcode.com/Gamez0/) | -| [JeongDaHyeon](https://github.com/JeongDaHyeon)
| South Korea | Java | [GitHub](https://github.com/JeongDaHyeon) | +| [JeongDaHyeon](https://github.com/JeongDaHyeon)
| South Korea | Java | [GitHub](https://github.com/JeongDaHyeon) | +[Aysia](https://www.linkedin.com/in/aysiaelise/) ![Hi](https://avatars.githubusercontent.com/u/70167431?s=460&u=1637be8636b6db6e35343ed9c1318c23e909b463&v=4) | USA | JavaScript | [GitHub](https://github.com/aysiae)
⬆️ Back to Top