题解 | #二叉树的下一个结点#
二叉树的下一个结点
https://www.nowcoder.com/practice/9023a0c988684a53960365b889ceaf5e
import java.util.*;
public class Solution {
ArrayList<TreeLinkNode> nodeList = new ArrayList<TreeLinkNode>();
public TreeLinkNode GetNext(TreeLinkNode pNode) {
// 获取根节点
TreeLinkNode prdNode = pNode;
while (prdNode.next != null) {
prdNode = prdNode.next;
}
//进行中序遍历
inorderSort(prdNode);
//集合遍历找到下一个子节点数值
for (int i = 0; i < nodeList.size() - 1; i++) {
if (pNode == nodeList.get(i)) {
return nodeList.get(i + 1);
}
}
return null;
}
void inorderSort(TreeLinkNode prdNode) {
if (prdNode != null) {
inorderSort(prdNode.left);
nodeList.add(prdNode);
inorderSort(prdNode.right);
}
}
}
