ツリーのノードを取る関数を書きたいです。preOrder で取得したノードの後に次にアクセスするノードを返す必要があります。私はこのコードを書きました:(このコードは左の子を検索して返します。一時に左の子がなく、右の子がある場合、この関数は右の子を返します。ただし、ノードが葉で子がない場合、親を取得します正しい子を持つノードを取得するまで。)
public Node fineNextPreOrder(Node temp)
{
if(temp.left!=null)
return temp.left;
else if((temp.left==null)&&(temp.right!=null))
return temp.right;
else if((temp.left==null)&&(temp.right==null))
{
while((temp!=root)&&(!((temp.parent.left!=null)&&(temp.parent.left==temp)&&(temp.parent.right!=null))))
temp = temp.parent;
if(temp != root)
return temp.parent.right;
}
return null;
}
それは正しく機能しますが、再帰的にしたいです。
誰でもこれで私を助けてもらえますか??
ご清聴ありがとうございました。