-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution8.java
More file actions
29 lines (28 loc) · 987 Bytes
/
Solution8.java
File metadata and controls
29 lines (28 loc) · 987 Bytes
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
package com.usher.algorithm.offer;
/**
* @Author: Usher
* @Description:
*
* 二叉树的下一个节点:找出中序遍历的下一个节点,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
* 1,如果一个节点的右子树不为空,那么该节点的下一个节点是右子树的最左节点
* 2,否则向上找第一个左链接指向的树包含该节点的祖先节点
*/
public class Solution8 {
public TreeLinkNode GetNext(TreeLinkNode pNode){
if (pNode.right != null){
TreeLinkNode node = pNode.right;
while (node.left != null){
node = node.left;
}
return node;
}else {
while (pNode.next != null){//父亲节点
TreeLinkNode parent = pNode.next;
if (parent.left == pNode)
return parent;
pNode = pNode.next;
}
}
return null;
}
}