关于树的一些回忆

先序abcdefgh  中序cbdaegfh  求树,和后续排放  

这么简单的一道算法题目,估计计算机系的应届毕业生上过数据结构课的。基本上是信手拈来了

一早上花了20分钟多,才回忆起树遍历的一些算法和原理。

其实就是用递归的方式去处理。

原理解决方法  

public static void preOrder(BinaryTree root){  //先根遍历

if(root!=null){

System.out.print(root.data+"-");

preOrder(root.left);

preOrder(root.right);

}

}

public static void inOrder(BinaryTree root){     //中根遍历

if(root!=null){

inOrder(root.left);

System.out.print(root.data+"--");

inOrder(root.right);

}

}

public static void postOrder(BinaryTree root){    //后根遍历

if(root!=null){

postOrder(root.left);

postOrder(root.right);

System.out.print(root.data+"---");

}

}

通过这个上面的原理理解基本上大家都能推算出整个树的结构了。

好吧,图片歪了。忽略这个细节。。我只是记录一下。我花了20分搞了这题目。

顺便回忆一下 树的遍历罢了。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容