Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 04b5334

Browse files
Add Solution.java to problems 0671
1 parent 3388586 commit 04b5334

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public int findSecondMinimumValue(TreeNode root) {
12+
if (root == null || root.left == null) return -1;
13+
int limit = Integer.MAX_VALUE;
14+
Stack<TreeNode> stack = new Stack<>();
15+
stack.push(root);
16+
while (!stack.isEmpty()) {
17+
TreeNode node = stack.pop();
18+
if (node.val != root.val) {
19+
limit = Math.min(limit, node.val - root.val);
20+
}
21+
if (node.left != null) {
22+
stack.push(node.left);
23+
stack.push(node.right);
24+
}
25+
}
26+
return limit == Integer.MAX_VALUE ? -1 : root.val + limit;
27+
}
28+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /