Skip to content

Commit 912d72f

Browse files
authored
Min depth (codedecks-in#157)
* min depth of binary tree * added comments, updated readme
1 parent e92b38c commit 912d72f

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
14+
// PROBLEM:
15+
// Given a binary tree, find its minimum depth.
16+
// The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
17+
18+
// test cases:
19+
// [3,9,20,null,null,15,7] -- output: 2
20+
// [2,null,3,null,4,null,5,null,6] -- output: 5
21+
// [0] -- output: 1
22+
// [] -- output: 0
23+
24+
25+
var minDepth = function(root) {
26+
if(root) {
27+
return traversal(root, 1);
28+
} else { return 0 }
29+
};
30+
31+
// traversal of the tree is recursive to ensure travel down
32+
// both the right and left children nodes
33+
var traversal = (root, depth) => {
34+
let current = root;
35+
let right;
36+
let left;
37+
if(!current) {
38+
return null;
39+
// this checks if the current node is a leaf node
40+
} if (!current.right && !current.left) {
41+
return depth;
42+
}
43+
right = traversal(current.right, depth+1)
44+
left = traversal(current.left, depth+1)
45+
if(!right) {
46+
return left;
47+
} else if(!left) {
48+
return right;
49+
} else {
50+
if(right < left) {
51+
return right
52+
} else {
53+
return left
54+
}
55+
}
56+
}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
320320
| 127 | [Word Ladder](https://leetcode.com/problems/word-ladder/) | [Java](./Java/word-ladder.java) | O(N^2 \* M) | O(N \* M) | Medium | BFS |
321321
| 994 | [Rotten Oranges](https://leetcode.com/problems/rotting-oranges/) | [Python](./Python/994_Rotting_Oranges.py) | O(N \* M) | O(N \* M) | Medium | BFS |
322322
| 743 | [Network Delay Time](https://leetcode.com/problems/network-delay-time/) | [C++](./C++/Network-delay-time.cpp) | _O(V+E))_ | O(V) | Medium | BFS |
323-
324-
323+
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
325324
<br/>
326325
<div align="right">
327326
<b><a href="#algorithms">⬆️ Back to Top</a></b>
@@ -481,7 +480,8 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
481480
| [Franchis N. Saikia](https://github.com/Francode007) <br> <img src="https://avatars0.githubusercontent.com/u/63102937?s=400&v=4" width="100" height="100"> | India | C++ | [Github](https://github.com/Francode007) |
482481
| [Yarncha](https://github.com/yarncha) <br> <img src="https://avatars0.githubusercontent.com/u/33623182?s=400&v=4" width="100" height="100"> | South Korea | C++ | [LeetCode](https://leetcode.com/yamcha/) |
483482
| [Gamez0](https://github.com/Gamez0) <br> <img src="https://avatars3.githubusercontent.com/u/34051876?s=400&v=4" width="100" height="100"> | South Korea | Python | [LeetCode](https://leetcode.com/Gamez0/) |
484-
| [JeongDaHyeon](https://github.com/JeongDaHyeon) <br> <img src="https://avatars0.githubusercontent.com/u/48541114?s=460&v=4" width="100" height="100"> | South Korea | Java | [GitHub](https://github.com/JeongDaHyeon) |
483+
| [JeongDaHyeon](https://github.com/JeongDaHyeon) <br> <img src="https://avatars0.githubusercontent.com/u/48541114?s=460&v=4" width="100" height="100"> | South Korea | Java | [GitHub](https://github.com/JeongDaHyeon) |
484+
[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)
485485
<br/>
486486
<div align="right">
487487
<b><a href="#algorithms">⬆️ Back to Top</a></b>

0 commit comments

Comments
 (0)