二叉树遍历

来源:互联网 发布:管道应力分析软件 编辑:程序博客网 时间:2024/06/03 10:15
 二叉树遍历分为三种   先序遍历  中序遍历  后序遍历

            实质就是递归算法

    先序遍历: public void preOrder(Bitree root)

                        {

                               if(root != null )

                              {

                                        visit(root.date);    //访问根节点

                                       preOrder(root.lChild) ;  //左子树

                                       preOrder(root.rChild);  //又子树

                               }

                         }

   中序遍历:

                     public void inOrder(Bitree root)

                        {

                               if(root != null )

                              {

                                         inOrder(root.lChild) ;  //左子树

                                        visit(root.date);    //访问根节点

                                         inOrder(root.rChild);  //又子树

                               }

                         }

 后续遍历;

                    public void postOrder(Bitree root)

                        {

                               if(root != null )

                              {

                                       postOrder(root.lChild) ;  //左子树

                                       postOrder(root.rChild);  //又子树

                                         visit(root.date);    //访问根节点

                               }

                         }

                 

原创粉丝点击