編寫一個遞歸算法,刪除二叉樹中所有葉子結點。
void foo(Node * node)
{
if (node->left != null)
{
if (node->left->left == null && node->left->right == null)
{
delete(node->left);
node->left = null;
}
else
{
foo(node->left);
}
}
if (node->right != null)
{
if (node->right->left == null && node->right->right == null)
{
delete(node->right);
node->right = null;
}
else
{
foo(node->right);
}
}
}