Skip to content

Commit 74e6826

Browse files
committed
fd
1 parent e93a404 commit 74e6826

File tree

4 files changed

+63
-10
lines changed

4 files changed

+63
-10
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
22

33
android {
44
compileSdkVersion 24
5-
buildToolsVersion '25.0.0'
5+
buildToolsVersion '26.0.2'
66
defaultConfig {
77
applicationId "com.inuker.leetcode"
88
minSdkVersion 18

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.3.0'
8+
classpath 'com.android.tools.build:gradle:3.0.0'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files

solution/src/main/java/com/inuker/solution/InorderSuccessorInBST.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,29 @@ public TreeNode inorderSuccessor2(TreeNode root, TreeNode p) {
5656
}
5757
return res;
5858
}
59+
60+
/**
61+
* http://www.geeksforgeeks.org/?p=9999
62+
* 给定Node,求其successor,步骤如下:
63+
* 1, 如果Node.right != null,则在Node.right中找最小的那个节点,即从Node.right开始,最左下角的节点
64+
* 2, 如果Node.right == null,则不断往parent走,直到当前节点是其parent的左节点为止,其parent即为给定Node的successor
65+
private TreeNode inOrderSuccessor(TreeNode root, TreeNode node) {
66+
if (node.right != null) {
67+
return minValue(node.right);
68+
}
69+
70+
TreeNode parent = node.parent;
71+
while (parent != null && node == parent.right) {
72+
node = parent;
73+
parent = parent.parent;
74+
}
75+
return parent;
76+
}
77+
78+
private TreeNode minValue(TreeNode node) {
79+
while (node.left != null) {
80+
node = node.left;
81+
}
82+
return node;
83+
}*/
5984
}

test/src/main/java/com/inuker/test/main.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.inuker.solution.BinaryTreeInorderTraversal;
44
import com.leetcode.library.TreeNode;
55

6+
import java.util.LinkedList;
67
import java.util.List;
8+
import java.util.Stack;
79

810
public class main {
911

@@ -20,16 +22,42 @@ public static void main(String[] args) {
2022
}
2123
}
2224

23-
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
24-
TreeNode node = null;
25-
while (root != null) {
26-
if (p.val < root.val) {
27-
node = root;
28-
root = root.left;
25+
public List<Integer> closestKValues(TreeNode root, double target, int k) {
26+
Stack<Integer> smaller = new Stack<>();
27+
Stack<Integer> larger = new Stack<>();
28+
helper(root, smaller, target, true);
29+
helper(root, larger, target, false);
30+
List<Integer> result = new LinkedList<>();
31+
for (int i = 0; i < k; i++) {
32+
if (smaller.isEmpty()) {
33+
result.add(larger.pop());
34+
} else if (larger.isEmpty()) {
35+
result.add(smaller.pop());
36+
} else if (Math.abs(smaller.peek() - target) < Math.abs(larger.peek() - target)) {
37+
result.add(smaller.pop());
2938
} else {
30-
root = root.right;
39+
result.add(larger.pop());
40+
}
41+
}
42+
return result;
43+
}
44+
45+
private void helper(TreeNode root, Stack<Integer> stack0, double target, boolean smaller) {
46+
Stack<TreeNode> stack = new Stack<>();
47+
while (!stack.isEmpty() || root != null) {
48+
if (root != null) {
49+
stack.push(root);
50+
root = smaller ? root.left : root.right;
51+
} else {
52+
root = stack.pop();
53+
54+
if ((smaller && root.val >= target) || (!smaller && root.val < target)) {
55+
return;
56+
}
57+
58+
stack0.push(root.val);
59+
root = smaller ? root.right : root.left;
3160
}
3261
}
33-
return node;
3462
}
3563
}

0 commit comments

Comments
 (0)