java中序遍历二叉树下一个节点

xiaoxiao2021-02-27  867

 public TreeLinkNode GetNext(TreeLinkNode pNode) { if(pNode==null) return null; TreeLinkNode right = pNode.right;//右子树 TreeLinkNode result = null; //节点有右子树,则为右子树总最左边的节点 if(right!=null){ while(right.left!=null) right = right.left; result = right; }else{//没有右子树 TreeLinkNode f = pNode.next;//父节点 if(f==null);//根节点 if(f!=null){//存在父节点 TreeLinkNode fLeft = f.left; if(fLeft==pNode){//在左子树上 result= f; }else{//在右子树上 //找到根节点 TreeLinkNode root=f.next; while(root.next!=null){ f = f.next; root=f.next; } TreeLinkNode rootleft = root.left; if(f==rootleft) result = root; } } } return result; }
转载请注明原文地址: https://www.6miu.com/read-92.html

最新回复(0)