-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKthNode.java
More file actions
52 lines (45 loc) · 1.15 KB
/
KthNode.java
File metadata and controls
52 lines (45 loc) · 1.15 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
49
50
51
52
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
import java.util.Stack;
public class Solution {
int count=0;
TreeNode KthNode(TreeNode pRoot, int k)
{
//中序遍历二叉树,得到从小到大的序列,压入栈,出栈得到第k大结点
//判空
if(count>k||pRoot==null){
return null;
}
TreeNode p=pRoot;
//实例化栈
Stack<TreeNode> LDRStack=new Stack<TreeNode>();
//初始化结果结点
TreeNode kthNode=null;
while(p!=null||!LDRStack.isEmpty()){
while(p!=null){
//压入栈,遍历左子树
LDRStack.push(p);
p=p.left;
}
//结点出栈
TreeNode node=LDRStack.pop();
System.out.print(node.val+",");
//计数
count++;
if(count==k){
kthNode=node;
}
//遍历右子树
p=node.right;
}
return kthNode;
}
}