Skip to content

Commit 05250ef

Browse files
committed
671_Second_Minimum_Node_In_a_Binary_Tree
1 parent 212ea0c commit 05250ef

3 files changed

+86
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ Also, there are open source implementations for basic data structs and algorithm
165165
| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/617_Merge_Two_Binary_Trees.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/617_Merge_Two_Binary_Trees.java) | Traverse both trees Recursion & Iterative (stack) |
166166
| 628 | [Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/628_Maximum_Product_of_Three_Numbers.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/628_Maximum_Product_of_Three_Numbers.java) | Actually, we should only care about min1, min2 and max1-max3, to find these five elements, we can use 1. Brute force, O(n^3) and O(1)<br>2. Sort, O(nlogn) and O(1)<br>3. Single scan with 5 elements keep, O(n) and O(1) |
167167
| 654 | [Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/654_Maximum_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/654_Maximum_Binary_Tree.java) | 1. Divide and conquer, recursive, O(n^2)<br>2. Monotonic stack, O(n) |
168+
| 671 | [Second Minimum Node In a Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/671_Second_Minimum_Node_In_a_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/671_Second_Minimum_Node_In_a_Binary_Tree.java) | Note that min value is root: 1. Get all values then find result, O(n) and O(n)<br>2. BFS or DFS traverse the tree, then find the reslut, O(n) and O(n)|
168169
| 674 | [Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/674_Longest_Continuous_Increasing_Subsequence.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/674_Longest_Continuous_Increasing_Subsequence.java) | Scan nums once, check nums[i] < nums[i+1], if not reset count, O(n) and O(1) |
169170
| 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/680_Valid_Palindrome_II.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/680_Valid_Palindrome_II.java) | Recursively check s[left == end, when not equal delete left or right. |
170171
| 692 | [Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/692_Top_K_Frequent_Words.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/692_Top_K_Frequent_Words.java) | 1. Sort based on frequency and alphabetical order, O(nlgn) and O(n)<br>2. Find top k with Heap, O(nlogk) and O(n) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
/*public void dfs(TreeNode root, Set<Integer> uniques) {
3+
if (root != null) {
4+
uniques.add(root.val);
5+
dfs(root.left, uniques);
6+
dfs(root.right, uniques);
7+
}
8+
}
9+
public int findSecondMinimumValue(TreeNode root) {
10+
// Brute force
11+
Set<Integer> uniques = new HashSet<Integer>();
12+
dfs(root, uniques);
13+
14+
int min1 = root.val;
15+
long ans = Long.MAX_VALUE;
16+
for (int v : uniques) {
17+
if (min1 < v && v < ans) ans = v;
18+
}
19+
return ans < Long.MAX_VALUE ? (int) ans : -1;
20+
}*/
21+
22+
public int findSecondMinimumValue(TreeNode root) {
23+
if (root == null) return -1;
24+
Stack<TreeNode> stack = new Stack<TreeNode>();
25+
int min_val = root.val;
26+
int ans = Integer.MAX_VALUE;
27+
stack.push(root);
28+
while (!stack.empty()) {
29+
TreeNode node = stack.pop();
30+
if (node == null) continue;
31+
if (node.val < ans && node.val > min_val) {
32+
ans = node.val;
33+
} else if (node.val == min_val) {
34+
stack.push(node.left);
35+
stack.push(node.right);
36+
}
37+
}
38+
return ans < Integer.MAX_VALUE ? ans : -1;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
# def findSecondMinimumValue(self, root):
10+
# """
11+
# :type root: TreeNode
12+
# :rtype: int
13+
# """
14+
# # Brute force
15+
# values = set()
16+
# self.dfs(root, values)
17+
# ans, min_value = float('inf'), root.val
18+
# for n in values:
19+
# if min_value < n and n < ans:
20+
# ans = n
21+
# return ans if ans < float('inf') else -1
22+
23+
# def dfs(self, root, values):
24+
# if not root:
25+
# return
26+
# values.add(root.val)
27+
# self.dfs(root.left, values)
28+
# self.dfs(root.right, values)
29+
30+
def findSecondMinimumValue(self, root):
31+
if not root:
32+
return -1
33+
ans = float('inf')
34+
min_val = root.val
35+
stack = [root]
36+
while stack:
37+
curr = stack.pop()
38+
if not curr:
39+
continue
40+
if min_val < curr.val < ans:
41+
ans = curr.val
42+
elif curr.val == min_val:
43+
stack.append(curr.left)
44+
stack.append(curr.right)
45+
return ans if ans < float('inf') else -1

0 commit comments

Comments
 (0)