Skip to content

Commit 0f215ea

Browse files
committed
Add solution #671
1 parent f721137 commit 0f215ea

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@
504504
668|[Kth Smallest Number in Multiplication Table](./0668-kth-smallest-number-in-multiplication-table.js)|Hard|
505505
669|[Trim a Binary Search Tree](./0669-trim-a-binary-search-tree.js)|Medium|
506506
670|[Maximum Swap](./0670-maximum-swap.js)|Medium|
507+
671|[Second Minimum Node In a Binary Tree](./0671-second-minimum-node-in-a-binary-tree.js)|Easy|
507508
680|[Valid Palindrome II](./0680-valid-palindrome-ii.js)|Easy|
508509
684|[Redundant Connection](./0684-redundant-connection.js)|Medium|
509510
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 671. Second Minimum Node In a Binary Tree
3+
* https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/
4+
* Difficulty: Easy
5+
*
6+
* Given a non-empty special binary tree consisting of nodes with the non-negative value,
7+
* where each node in this tree has exactly two or zero sub-node. If the node has two
8+
* sub-nodes, then this node's value is the smaller value among its two sub-nodes. More
9+
* formally, the property root.val = min(root.left.val, root.right.val) always holds.
10+
*
11+
* Given such a binary tree, you need to output the second minimum value in the set made
12+
* of all the nodes' value in the whole tree.
13+
*
14+
* If no such second minimum value exists, output -1 instead.
15+
*/
16+
17+
/**
18+
* Definition for a binary tree node.
19+
* function TreeNode(val, left, right) {
20+
* this.val = (val===undefined ? 0 : val)
21+
* this.left = (left===undefined ? null : left)
22+
* this.right = (right===undefined ? null : right)
23+
* }
24+
*/
25+
/**
26+
* @param {TreeNode} root
27+
* @return {number}
28+
*/
29+
var findSecondMinimumValue = function(root) {
30+
return root ? (n => n === Infinity ? -1 : n)(traverse(root, root.val)) : -1;
31+
function traverse(node, min) {
32+
if (!node) return Infinity;
33+
if (node.val > min) return node.val;
34+
return Math.min(traverse(node.left, min), traverse(node.right, min));
35+
}
36+
};

0 commit comments

Comments
 (0)