【数据结构与算法】二叉树的遍历

一、三序遍历递归实现

    public void preOrder(TreeNode root,ArrayList<Integer> list){
        if(root==null) return;
        list.add(root.val);
        if(root.left!=null) preOrder(root.left,list);
        if(root.right!=null) preOrder(root.right,list);
    }
    public void inOrder(TreeNode root,ArrayList<Integer> list){
        if(root==null) return;
        if(root.left!=null) preOrder(root.left,list);
        list.add(root.val);
        if(root.right!=null) preOrder(root.right,list);
    }   
    public void postOrder(TreeNode root,ArrayList<Integer> list){
        if(root==null) return;
        if(root.left!=null) preOrder(root.left,list);
        if(root.right!=null) preOrder(root.right,list);
        list.add(root.val);
    }

二、三序遍历非递归实现

    public static ArrayList<Integer> preOrder(TreeNode root){
        ArrayList<Integer> list=new ArrayList<Integer>();
        if(root==null) return list;
        Stack<TreeNode> stack=new Stack<TreeNode>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode tmp=stack.pop();
            list.add(tmp.val);
            if(tmp.right!=null) stack.push(tmp.right);
            if(tmp.left!=null) stack.push(tmp.left);
        }
        return list;
    }
    public static ArrayList<Integer> preOrder2(TreeNode root){
        ArrayList<Integer> list=new ArrayList<Integer>();
        if(root==null) return list;
        Stack<TreeNode> stack=new Stack<TreeNode>();
        TreeNode tmp=root;
        while(tmp!=null||!stack.isEmpty()){
            while(tmp!=null){
                list.add(tmp.val);
                stack.push(tmp);
                tmp=tmp.left;
            }
            tmp=stack.pop();
            tmp=tmp.right;
        }
        return list;
    }
    public static ArrayList<Integer> inOrder(TreeNode root){
        ArrayList<Integer> list=new ArrayList<Integer>();
        if(root==null) return list;
        Stack<TreeNode> stack=new Stack<TreeNode>();
        TreeNode tmp=root;
        while(tmp!=null||!stack.isEmpty()){
            while(tmp!=null){
                stack.push(tmp);
                tmp=tmp.left;
            }
            tmp=stack.pop();
            list.add(tmp.val);
            tmp=tmp.right;
        }
        return list;
    }
    public static ArrayList<Integer> postOrder(TreeNode root){
        ArrayList<Integer> list=new ArrayList<Integer>();
        if(root==null) return list;
        Stack<TreeNode> stack=new Stack<TreeNode>();
        TreeNode tmp=root,pre=null;
        while(tmp!=null||!stack.isEmpty()){
            while(tmp!=null){
                stack.push(tmp);
                tmp=tmp.left;
            }
            TreeNode topRight=stack.peek().right;
            if(topRight==null||topRight==pre){
                pre=stack.pop();
                list.add(pre.val);
                tmp=null;
            }else{
                tmp=topRight;
            }
        }
        return list;
    }   
    public static ArrayList<Integer> postOrder2(TreeNode root){//双栈法不推荐
        ArrayList<Integer> list=new ArrayList<Integer>();
        if(root==null) return list;
        Stack<TreeNode> stack=new Stack<TreeNode>();
        Stack<TreeNode> out=new Stack<TreeNode>();
        TreeNode tmp=root;
        while(tmp!=null||!stack.isEmpty()){
            while(tmp!=null){
                stack.push(tmp);
                out.push(tmp);
                tmp=tmp.right;
            }
            tmp=stack.pop();
            tmp=tmp.left;
        }
        while(!out.isEmpty()){
            list.add(out.pop().val);
        }
        return list;
    }

三、Morris线索二叉树遍历

这里没看太懂,Mark一下。Morris线索二叉树遍历

四、层次遍历

    public static ArrayList<Integer> bfs(TreeNode root){
        ArrayList<Integer> list=new ArrayList<Integer>();
        if(root==null) return list;
        Queue<TreeNode> queue=new LinkedList<TreeNode>();
        queue.add(root);
        TreeNode tmp=null;
        while(!queue.isEmpty()){
            tmp=queue.poll();
            list.add(tmp.val);
            if(tmp.left!=null) queue.add(tmp.left);
            if(tmp.right!=null) queue.add(tmp.right);
        }
        return list;
    }
    public static ArrayList<Integer> bfs2(TreeNode root){
        ArrayList<Integer> list=new ArrayList<Integer>();
        if(root==null) return list;
        ArrayList<TreeNode> queue=new ArrayList<TreeNode>();
        queue.add(root);
        TreeNode tmp=null;
        int start=0,end=queue.size();
        while(start<end){
            tmp=queue.get(start);
            list.add(tmp.val);
            if(tmp.left!=null) queue.add(tmp.left);
            if(tmp.right!=null) queue.add(tmp.right);
            start++;
            if(start==end){
                end=queue.size();
            }
        }
        return list;
    }

五、分层遍历和按之字形遍历

剑指offer上的例题分层遍历和按之字形遍历

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值