-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextTreeNode58.java
More file actions
48 lines (47 loc) · 1.37 KB
/
NextTreeNode58.java
File metadata and controls
48 lines (47 loc) · 1.37 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 offer;
/**
* 二叉树某节点的下一个节点
*
* 根据offer上讲的是 中序线索化。还有后序线索化等
* @author fqx
*
*/
class TreeLinkNode {
int val;
TreeLinkNode left = null;
TreeLinkNode right = null;
TreeLinkNode next = null;
TreeLinkNode(int val) {
this.val = val;
}
}
public class NextTreeNode58 {
public TreeLinkNode GetNext(TreeLinkNode pNode)
{
TreeLinkNode target = null;
if(pNode == null) return null;
/*
* if pNode has right child, then next node must in the right
* subTree. so the next node is the subTree's most left node
*/
if(pNode.right != null){
target = pNode.right;
while(target.left != null){
target = target.left;
}
}else if(pNode.next != null){
target = pNode.next;
TreeLinkNode cur = pNode;
/*
* if pNode has no right
* and pNode is parent's right
* so to find a node is the node's parent's left node
*/
while(target != null && target.left != cur){
cur = target;
target = target.next;
}
}
return target;
}
}