zoukankan      html  css  js  c++  java
  • 二叉树前序中序后续(递归和非递归遍历)

    leetcode:好好A吧少年

    代码描述了六种二叉树遍历方式分别是:二叉树前序中序后续(递归和非递归方式):

    注意遍历方式就ok:

    前序和中序非递归遍历方式一样,只是加入的输出语句不同,算法描述:

    1.先建立一个栈,存储根节点root,并设立辅助节点tmp

    2.当栈不为空或者节点不为空时遍历,首先查找当前节点的左节点,循环查找,直到节点左节点为空,此时,弹出栈顶节点,此节点的左孩子为空,并重新赋值当前节点的右孩子为当前节点

    3.如果当前节点为空,则,跳过加入栈,否者将当前节点加入栈,并重新for循环

    后续遍历非递归方法是:

    1. 建立一个栈用于存储头结点

    2.将树节点增加一个标识位,

    3. 依次添加栈顶节点的左右孩子节点,并设当前节点已经访问,否者跳过当前就节点

    代码如下:

    package com.bupt.acm.leetcode;
    
    import java.util.HashMap;
    import java.util.Stack;
    
    /**
     * 树的六种遍历方式
     * @author DELL
     *
     */
    public class TreeTravel {
    
        private static class TreeNode{
            int val;
            TreeNode left=null;
            TreeNode right=null;
            boolean isVisited=false;
            public TreeNode(int x){
                val=x;
            }
        }
        /**
         * 先序递归遍历
         */
        public void preOrder(TreeNode root){
            if(root!=null){
                System.out.print(root.val+" ");
                preOrder(root.left);
                preOrder(root.right);
            }
        }
        /**
         * 先序非递归遍历
         */
        public void preOrderIterally(TreeNode root){
            Stack<TreeNode> stack=new Stack<TreeNode>();
            if(root==null)
                return;
            stack.push(root);
            System.out.print(root.val+" ");
            TreeNode tmp=root;
            
            while(!stack.isEmpty()||tmp!=null){
                while(tmp!=null&&tmp.left!=null){
                    stack.push(tmp.left);
                    System.out.print(tmp.left.val+" ");
                    tmp=tmp.left;
                }
                if(!stack.isEmpty()){
                    tmp=stack.pop();
                }
                tmp=tmp.right;
                if(tmp!=null){
                    stack.push(tmp);
                    System.out.print(tmp.val+" ");
                }
            }
        }
        /**
         * 中序递归遍历
         */
        public void inOrder(TreeNode root){
            if(root!=null){
                inOrder(root.left);
                System.out.print(root.val+" ");
                inOrder(root.right);
            }
        }
        /**
         * 中序非递归遍历
         */
        public void inOrderIterally(TreeNode root){
            if(root==null){
                return;
            }
            Stack<TreeNode> stack=new Stack<TreeNode>();
            stack.push(root);
            TreeNode tmp=root;
            while(!stack.isEmpty()||tmp!=null){
                while(tmp!=null&&tmp.left!=null){
                    stack.push(tmp.left);
                    tmp=tmp.left;
                }
                if(!stack.isEmpty()){
                    tmp=stack.pop();
                    System.out.print(tmp.val+" ");
                }
                tmp=tmp.right;
                if(tmp!=null){
                    stack.push(tmp);
                }
            }
        }
        /**
         * 后续递归遍历
         */
        public void lastOrder(TreeNode root){
            if(root!=null){
                lastOrder(root.left);
                lastOrder(root.right);
                System.out.print(root.val+" ");
            }
        }
        /**
         * 后续非递归遍历
         * @param args
         */
        public void lastOrderIterally(TreeNode root){
            if(root==null)
                return;
            TreeNode tmp=root;
            Stack<TreeNode> stack=new Stack<TreeNode>();
            stack.push(root);
            while(!stack.isEmpty()){
                tmp=stack.peek();
                if(!tmp.isVisited){
                    if(tmp.right!=null){
                        stack.push(tmp.right);
                    }
                    if(tmp.left!=null){
                        stack.push(tmp.left);
                    }
                    tmp.isVisited=true;
                }else{
                    System.out.print(stack.pop().val+" ");
                }            
            }
        }
        public static void main(String[] args) {
            TreeTravel tt=new TreeTravel();
            //左子树
            TreeNode root=new TreeNode(0);
            root.left=new TreeNode(1);
            root.right=new TreeNode(2);
            root.left.left=new TreeNode(3);
            root.left.left.right=new TreeNode(4);
            root.left.left.right.left=new TreeNode(5);
            root.left.left.right.right=new TreeNode(6);
            //右子树
            root.right.left=new TreeNode(7);
            root.right.right=new TreeNode(8);
            root.right.right.left=new TreeNode(9);
            root.right.right.right=new TreeNode(10);
            System.out.println("前序递归输出:");
            tt.preOrder(root);
            System.out.println("
    前序非递归输出:");
            tt.preOrderIterally(root);
            System.out.println("
    中序递归输出:");
            tt.inOrder(root);
            System.out.println("
    中序非递归输出:");
            tt.inOrderIterally(root);
            System.out.println("
    后序递归输出:");
            tt.lastOrder(root);
            System.out.println("
    后序非递归输出:");
            tt.lastOrderIterally(root);
        }
    }
  • 相关阅读:
    java.lang.NoSuchMethodError
    asm相关内容想下载(包括 jar 包)
    Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/objectweb/asm/Type
    用Navicat连接mysql报错:2003-Can't connect to MySql server on '10.100.0.109'(10039)
    The type java.lang.reflect.AnnotatedElement cannot be resolved. It is indirectly referenced from required .class files
    The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
    交通测速方式
    卡口和电子警察的区别
    Myeclipse连接Mysql数据库时报错:Error while performing database login with the pro driver:unable
    在window上安装mysql
  • 原文地址:https://www.cnblogs.com/csxf/p/3735902.html
Copyright © 2011-2022 走看看