思路:先用.next找到整棵树的根结点,并对这棵树采取用中序遍历和存入list中。最后再for循环找到下一个结点
import java.util.*; public class Solution { ArrayList<TreeLinkNode> list = new ArrayList<TreeLinkNode>(); public TreeLinkNode GetNext(TreeLinkNode pNode) { if(pNode==null)return null; TreeLinkNode Dad = findDad(pNode); inOrder(Dad); for(int i=0;i<list.size();i++){ if(pNode == list.get(i)&&i<(list.size()-1)){ return list.get(i+1); } } return null; } public TreeLinkNode findDad(TreeLinkNode pNode){ while(pNode.next!=null)pNode=pNode.next; return pNode; } public void inOrder(TreeLinkNode pNode){ if(pNode==null)return; inOrder(pNode.left); list.add(pNode); inOrder(pNode.right); } }