forked from geekxh/hello-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
54 lines (46 loc) · 1.07 KB
/
Solution.java
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
53
54
/*
public class TreeLinkNode {
int val;
TreeLinkNode left = null;
TreeLinkNode right = null;
TreeLinkNode next = null;
TreeLinkNode(int val) {
this.val = val;
}
}
*/
/**
* @author Anonymous
* @since 2019/10/28
*/
public class Solution {
/**
* 获取中序遍历结点的下一个结点
*
* @param pNode 某个结点
* @return pNode的下一个结点
*/
public TreeLinkNode GetNext(TreeLinkNode pNode) {
if (pNode == null) {
return null;
}
if (pNode.right != null) {
TreeLinkNode t = pNode.right;
while (t.left != null) {
t = t.left;
}
return t;
}
// 须保证 pNode.next 不为空,否则会出现 NPE
if (pNode.next != null && pNode.next.left == pNode) {
return pNode.next;
}
while (pNode.next != null) {
if (pNode.next.left == pNode) {
return pNode.next;
}
pNode = pNode.next;
}
return null;
}
}