Skip to content

Commit 8d568d5

Browse files
author
Lei Cao
committed
285 inorder successor in BST
1 parent 4891b26 commit 8d568d5

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package inorderSuccessorInBST;
2+
3+
/**
4+
* Created by leicao on 5/10/15.
5+
*/
6+
public class TreeNode {
7+
int val;
8+
TreeNode left;
9+
TreeNode right;
10+
TreeNode(int x) { val = x; }
11+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Source : http://www.lintcode.com/en/problem/inorder-successor-in-bst/
2+
// https://leetcode.com/problems/inorder-successor-in-bst/
3+
// Inspired by : http://www.jiuzhang.com/solutions/inorder-successor-in-bst/
4+
// Author : Lei Cao
5+
// Date : 2015-10-06
6+
7+
/**********************************************************************************
8+
*
9+
* Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
10+
*
11+
* Example
12+
* Given tree = [2,1] and node = 1:
13+
*
14+
* 2
15+
* /
16+
* 1
17+
* return node 2.
18+
*
19+
* Given tree = [2,1,3] and node = 2:
20+
*
21+
* 2
22+
* / \
23+
* 1 3
24+
*
25+
* return node 3.
26+
*
27+
* Note
28+
* If the given node has no in-order successor in the tree, return null.
29+
*
30+
* Challenge
31+
* O(h), where h is the height of the BST.
32+
**********************************************************************************/
33+
34+
package inorderSuccessorInBST;
35+
36+
public class inorderSuccessorInBST {
37+
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
38+
TreeNode successor = null;
39+
if (root == null) {
40+
return null;
41+
}
42+
while (root != null && root.val != p.val) {
43+
if (root.val > p.val) {
44+
successor = root;
45+
root = root.left;
46+
} else {
47+
root = root.right;
48+
}
49+
}
50+
51+
if (root == null) {
52+
return null;
53+
}
54+
55+
if (root.right == null) {
56+
return successor;
57+
}
58+
59+
root = root.right;
60+
while (root.left != null) {
61+
root = root.left;
62+
}
63+
return root;
64+
}
65+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package inorderSuccessorInBST;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
7+
import static org.junit.Assert.*;
8+
9+
/**
10+
* Created by leicao on 7/10/15.
11+
*/
12+
public class inorderSuccessorInBSTTest {
13+
14+
@Test
15+
public void testInorderSuccessor() throws Exception {
16+
ArrayList<TreeNode> inputes = new ArrayList<TreeNode>();
17+
ArrayList<TreeNode> targets = new ArrayList<TreeNode>();
18+
19+
TreeNode n0 = new TreeNode(2);
20+
TreeNode n1 = new TreeNode(1);
21+
TreeNode n2 = new TreeNode(3);
22+
n0.left = n1;
23+
n0.right = n2;
24+
inputes.add(n0);
25+
targets.add(n2);
26+
27+
TreeNode nn0 = new TreeNode(2);
28+
TreeNode nn1 = new TreeNode(1);
29+
nn0.left = nn1;
30+
inputes.add(nn0);
31+
targets.add(nn1);
32+
33+
TreeNode t0 = new TreeNode(1);
34+
TreeNode t1 = new TreeNode(2);
35+
t0.right = t1;
36+
inputes.add(t0);
37+
targets.add(t0);
38+
39+
40+
ArrayList<TreeNode> results = new ArrayList<TreeNode>();
41+
results.add(null);
42+
results.add(nn0);
43+
results.add(t1);
44+
45+
for (int i = 0; i < results.size(); i++) {
46+
inorderSuccessorInBST finder = new inorderSuccessorInBST();
47+
TreeNode node = finder.inorderSuccessor(inputes.get(i), targets.get(i));
48+
System.out.println(node);
49+
boolean result = node == results.get(i) || node.val == results.get(i).val;
50+
assertTrue(result);
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)