013-二叉树的遍历操作

前提

	public class TreeNode {
	    int val;
	    TreeNode left;
	    TreeNode right;
	
	    public TreeNode() {
	    }
	
	    public TreeNode(int val) {
	        this.val = val;
	    }
	
	    public TreeNode(int val, TreeNode left, TreeNode right) {
	        this.val = val;
	        this.left = left;
	        this.right = right;
	    }
	}

递归遍历实现

(1)前序遍历

	public static void preorder(TreeNode root)
	    {
	        if(root==null)return;
	        System.out.println(root.val);
	        preorder(root.left);
	        preorder(root.right);
	    }

中序遍历

	public static void midorder(TreeNode root)
	    {
	        if(root==null)return;
	        midorder(root.left);
	        System.out.println(root.val);
	        midorder(root.right);
	    }

后序遍历

	 public static void postorder(TreeNode root)
	    {
	        if(root==null)return ;
	        postorder(root.left);
	        postorder(root.right);
	        System.out.println(root.val);
	    }

层序遍历

	 public static void levelorder(TreeNode root,int i,ArrayList list)
	    {
	        if(root==null)return ;
	//        list.size()<i; 会出现报错
	//        对list进行填充
	        int length =list.size();
	        if(length<=i)
	        {
	            for(int j = 0;j<=i-length;j++)
	            {
	                list.add(length+j,null);
	            }
	        }
	        list.set(i,root.val);
	        levelorder(root.left,i*2,list);
	        levelorder(root.right,i*2+1,list);
	    }

迭代实现遍历

前序遍历

//    迭代实现前序遍历
    public static void preorder(TreeNode root)
    {
        if(root!=null)
        {
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);
            while(!stack.isEmpty())
            {
                root = stack.pop();
                if(root!=null)
                {
                    System.out.println(root.val);
//               栈的特点是后入先出
                    stack.push(root.right);
                    stack.push(root.left);
                }
            }
        }
    }

中序遍历

//    迭代实现中序遍历
    public static void midorder(TreeNode root)
    {
        if(root!=null)
        {
            Stack<TreeNode> stack = new Stack<>();
            while(!stack.isEmpty()||root!=null)
            {
                if(root!=null)
                {
                    //直到找到最左节点为止
                    stack.push(root);
                    root = root.left;
                }
                else
                {
                    //将内容进行取出
                    root = stack.pop();
                    //打印相应的值
                    System.out.println(root.val);
                    //开始找到的最左节点的右节点开始操作
                    root = root.right;
                }
            }
        }
    }

后序遍历

 //    后序遍历实现递归操作
    public static void postorder(TreeNode root)
    {
        if(root!=null)
        {
            Stack<TreeNode> stack = new Stack<>();
            TreeNode pre = null;
            while(!stack.isEmpty()||root!=null)
            {
                //一直找到最左节点
                while(root!=null)
                {
                    stack.push(root);
                    root = root.left;
                }
                //将找到的节点进行弹出
                root = stack.pop();
                if(root.right==null||root.right==pre)
                {
                    //只有该节点的右节点为空 或者已经打印过了
                    //才可以将此节点的内容进行打印输出
                    System.out.println(root.val);
                    //那么此时的前一个节点就是当前节点了
                    pre = root;
                    //为什么将此时的root设置成null
                    //为了避免该节点进入while的循环
                    root = null;
                }
                else
                {
                    //如果该节点不符合右节点为null 或者 右节点已经被打印这个条件
                    //那么还得将这个节点重新压入栈中
                    stack.push(root);
                    //对该节点的右半部分进行相应操作
                    root = root.right;
                }
            }
        }
    }

层序遍历

//    迭代实现层序遍历
    public static void levelOrder(TreeNode root)
    {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty())
        {
            TreeNode node = queue.poll();
            if(node!=null)
            {
                System.out.println(node.val);
                queue.add(node.left);
                queue.add(node.right);
            }
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值