のは、私はそうのように、単純なバイナリツリーノードのクラスを持っているとしましょう:Javaの
public class BinaryTreeNode {
public String identifier = "";
public BinaryTreeNode parent = null;
public BinaryTreeNode left = null;
public BinaryTreeNode right = null;
public BinaryTreeNode(BinaryTreeNode parent, String identifier)
{
this.parent = parent; //passing null makes this the root node
this.identifier = identifier;
}
public boolean IsRoot() {
return parent == null;
}
}
にはどうすれば再帰的に任意のサイズの木を横断することが可能である方法を追加します既存の各ノードを左から右に移動して、なしで、すでに通過したノードを再訪しますか?
この作品?:
public void traverseFrom(BinaryTreeNode rootNode)
{
/* insert code dealing with this node here */
if(rootNode.left != null)
rootNode.left.traverseFrom(rootNode.left);
if(rootNode.right != null)
rootNode.traverseFrom(rootNode.right);
}
これは以下の正解とよく似ています。 –
@PeterWooster - 右、各ノードからtraverseメソッドを呼び出して、ルートからではなく各ノードに対して再帰的に発生させることを除いて – RectangleEquals