二叉树的遍历(递归,非递归,层次)

非递归中序遍历:

//   对于结点root;
//   1)若其结点不为空,则将root入栈并将root的左孩子置为当前的root,然后对当前结点root再进行相同的处理;
//   2)若其左孩子为空,则取栈顶元素并进行出栈操作,访问该节点,然后将当前的root置为栈顶结点的右孩子
//   3)直到p为null并且栈为空则遍历结束
     // iterative
    public void inorderTraversal2(TreeNode root){
        Stack<TreeNode> stack = new Stack<>();
        List<Integer> list = new ArrayList<>();
        while(root!=null || !stack.isEmpty()){
            while( root!= null){
                stack.push( root);
                root = root. left;
            }
            if (! stack.isEmpty()) {
                  TreeNode topNode = stack.peek();
                  System. out.println( " "+ topNode. key);
                  stack.pop();
                 
                  if ( null != topNode. right) {
                            root= topNode. right;
                       }
                }
        }
    }


//   非递归中序遍历----这种不容易记忆和编写
     public void inorder_iterative_tree_walk(TreeNode root){
           Stack<TreeNode> stack = new Stack<>();
            while( root!= null || ! stack.isEmpty()){
                 if ( root != null) {
                      stack.push( root);
                      root = root. left;
                } else{
                     TreeNode top = stack.peek();
                     System. out.print( top. key+ " ");
                      stack.pop();
                      root = top. right; //访问右子树
                }
           }
     }
     

//   中序遍历---递归
     public void  inorder_tree_walk( TreeNode x){
            if ( x!= null) {
                inorder_tree_walk( x. left);
                System. out.print( x. key + " ");
                inorder_tree_walk( x. right);
           }
     }
     
//   前序遍历---递归
     public void  preorder_tree_walk( TreeNode x){
            if ( x!= null) {
                System. out.print( x. key + " ");
                preorder_tree_walk( x. left);
                preorder_tree_walk( x. right);
           }
     }
     
//   前序遍历---非递归
     public void preorder_iterative_tree_walk(TreeNode x){
           Stack<TreeNode > stack = new Stack<>();
            stack.push( x);
            while(! stack.isEmpty()){
                TreeNode top = stack.peek();
                System. out.print( top. key+ " ");
                 stack.pop();
                
                 if( top. right!= null){
                      stack.push( top. right);
                }
                
                 if ( top. left!= null) {
                      stack.push( top. left);
                }
           }
     }
     
//   后序遍历---递归版
     public void postorder_tree_walk(TreeNode x){
            if ( x!= null) {
                postorder_tree_walk( x. left);
                postorder_tree_walk( x. right);
                System. out.print( x. key+ " ");
           }
     }
     
//   后序遍历非递归
//   1.push根结点到第一个栈s中
//   2.从第一个栈s中弹出一个结点,并将其push到第二个output中
//   3.然后push结点的左孩子和右孩子到第一个栈s中
//   4.重复2,3过程直到栈s为空
     public void postorder_iterative_tree_walk(TreeNode x){
            if ( x== null) return ;
           Stack<TreeNode> stack = new Stack<>();
           Stack<TreeNode> output = new Stack<>();
            stack.push( x);
            while(! stack.isEmpty()){
                TreeNode top = stack.peek();
                 output.push( top);
                 stack.pop();
                
                 if ( top. left!= null) {
                      stack.push( top. left);
                }
                 if ( top. right != null) {
                      stack.push( top. right);
                }
           }
           
            while(! output.isEmpty()){
                System. out.print( output.peek(). key+ " ");
                 output.pop();
           }
     }
     
//   层次遍历
     public void level_order_tree_walk(TreeNode x){
            if ( x== null)     return ;
//java中队列的调用方法和c++的不一样
           LinkedBlockingQueue<TreeNode> queue = new LinkedBlockingQueue();
            queue.add( x);
            while(! queue.isEmpty()){
                TreeNode top = queue.peek();
                System. out.print( top. key+ " ");
                 queue.poll(); //移除元素
                 if ( top. left != null) {
                      queue.offer( top. left);
                }
                 if ( top. right != null) {
                      queue.offer( top. right);
                }
           }
     }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值