Skip to content

Commit 57ebe02

Browse files
committed
add biset and next inorder bst
1 parent d616bbc commit 57ebe02

File tree

6 files changed

+72
-0
lines changed

6 files changed

+72
-0
lines changed

first-bad-version/README.md

Whitespace-only changes.

first-bad-version/Solution.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* The isBadVersion API is defined in the parent class VersionControl.
2+
boolean isBadVersion(int version); */
3+
4+
public class Solution extends VersionControl {
5+
public int firstBadVersion(int n) {
6+
int good = 0; // not exists
7+
int bad = n;
8+
9+
for(;;) {
10+
11+
if(bad - good <= 1) return bad;
12+
13+
int t = (bad - good) / 2 + good; // fuck overflow
14+
15+
if(isBadVersion(t)){
16+
bad = t;
17+
} else {
18+
good = t;
19+
}
20+
}
21+
22+
}
23+
}

first-bad-version/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
layout: solution
3+
title: First Bad Version
4+
date: 2015-09-24 16:10:45+08:00
5+
leetcode_id: 278
6+
---
7+
{% include_relative README.md %}

inorder-successor-in-bst/README.md

Whitespace-only changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
public class Solution {
11+
12+
TreeNode leftMost(TreeNode root){
13+
if(root == null) return null;
14+
if(root.left != null) return leftMost(root.left);
15+
return root;
16+
}
17+
18+
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
19+
if(root == p) {
20+
return leftMost(p.right);
21+
}
22+
23+
if(p.val < root.val) {
24+
p = inorderSuccessor(root.left, p);
25+
26+
if(p == null){
27+
return root;
28+
}
29+
30+
return p;
31+
}
32+
33+
return inorderSuccessor(root.right, p);
34+
}
35+
}

inorder-successor-in-bst/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
layout: solution
3+
title: Inorder Successor in BST
4+
date: 2015-09-24 17:32:03+08:00
5+
leetcode_id: 285
6+
---
7+
{% include_relative README.md %}

0 commit comments

Comments
 (0)