順序付けられていない二分木があり、ルート x のサブツリーを削除する方法を実行する必要がありました。要素 x がバイナリ ツリーに複数回存在する場合、メソッドはルート x のサブツリーの 1 つだけを削除します (最初に見つかったもの)。削除が実行された場合、true を返します。要素 x がバイナリ ツリーに存在しない場合は、false を返します。したがって、方法は次のとおりです。
public class BinaryTree
{
protected class Node
{
Integer element;
Node left;
Node right;
Node(int element)
{
this.element = element;
left = right = null;
}
Node(int element, Node left, Node right)
{
this.element = element;
this.left = left;
this.right = right;
}
protected Node root;
public BinaryTree()
{
root = null;
}
private class BoolNode
{
boolean ft;
Node nodo;
BoolNode(boolean ft, Node nodo)
{
this.ft = ft;
this.nodo = nodo;
}
}
public boolean removeSubtree(int x)
{
BoolNode ris = removeSubtree(x, root);
//root = ...;
return ris.ft;
}
private BoolNode removeSubtree(int x, Node node)
{
return null;
}
}
始め方がわかりません。疑似コードでさえ..ありがとう!