How can I show post title by post catagory in sidebar as three level menu

<?php
$args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<li> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
?>

<?php wp_reset_query(); ?>
<ul>
<?php
$recent = new WP_Query("cat=".$category->term_id);
while($recent->have_posts()) : $recent->the_post();

$a = explode(' ',get_the_title());
?>

<li>
<a href="<?php the_permalink() ?>" ><?php echo $a[0].' '.$a[1].' '.$a[2].' '.$a[3].' '.$a[4]; ?></a>
</li>

<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
</li>
<?php

}
?>