-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathKthNodeInBST.java
More file actions
48 lines (42 loc) · 1.14 KB
/
KthNodeInBST.java
File metadata and controls
48 lines (42 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package _54;
import java.util.Stack;
import structure.TreeNode;
// 二叉搜索树的第 k 个结点
// 给定一棵二叉搜索树,请找出其中的第 k 大的结点。
public class KthNodeInBST {
public static TreeNode kthNode(TreeNode root, int k) {
Stack<TreeNode> stack = new Stack();
TreeNode p = root;
int count = 0;
while (p != null || !stack.empty()) {
while (p != null) {
stack.push(p);
p = p.left;
}
p = stack.pop();
count++;
if (count == k) {
return p;
}
p = p.right;
}
return null;
}
public static void main(String[] args) {
// 5
// / \
// 3 7
// / \ / \
// 2 4 6 8
TreeNode root = new TreeNode(5);
root.left = new TreeNode(3);
root.right = new TreeNode(7);
root.left.left = new TreeNode(2);
root.left.right = new TreeNode(4);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(8);
System.out.println(kthNode(root, 3).val); //4
System.out.println(kthNode(root, 6).val); //7
System.out.println(kthNode(root, 8)); //null
}
}