java实现二叉树常见遍历算法

最近在复习二叉树遍历相关方面的知识,查看书籍以及在网上搜集了一些资料,我把它整理出来,放在这里,供自己以后再看,也供大家参考参考!

二叉树遍历方法

 1.前序遍历(先根遍历)————访问根节点的操作发生在遍历其左右子树之前。
 2.中序遍历(中根遍历)————访问根节点的操作发生在遍历其左右子树之中。
 3.后序遍历(后根遍历)————访问根节点的操作发生在遍历其左右子树之后。

1.二叉树先序非递归遍历

    /*先序非递归遍历*/
    public ArrayList<Integer> preorderTraversal2(TreeNode root) {
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode newtree = stack.pop();
            list.add(newtree.val);
            if(newtree.right!=null)
                stack.push(newtree.right);
            if(newtree.left!=null)
                stack.push(newtree.left);
        }
        return list;
    }

2.先序递归遍历

    ArrayList<Integer> list =new ArrayList<Integer>();
    /*先序递归遍历*/
    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        if(root!=null){
            list.add(root.val);
            preorderTraversal(root.left);
            preorderTraversal(root.right);
        }
        return list;
    }

3.二叉树中序非递归遍历

/*中序非递归遍历*/
public ArrayList<Integer> inorderTraversal2(TreeNode root){
        Stack<TreeNode> stack = new Stack<TreeNode>();
        while(!stack.isEmpty()||root!=null){
            while(root!=null){
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            list.add(root.val);
            root = root.right;
        }
        return list;
    }

4.中序递归遍历

    /*递归中序遍历*/
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
            if(root!=null){
                inorderTraversal(root.left);
                list.add(root.val);
                inorderTraversal(root.right);
            }
            return list;
        }

5.二叉树后序非递归遍历

   /*非递归后序遍历*/
  public void postorderTraversa2(TreeNode root)     
    {
        Stack<TreeNode> s = new Stack<TreeNode>();
        TreeNode cur=null;                      //当前结点 
        TreeNode pre=null;                 //前一次访问的结点 
        s.push(root);
        while(!s.empty())
        {
            cur=s.peek();
            if((cur.left==null&&cur.right==null)||
               (pre!=null&&(pre==cur.left||pre==cur.right)))
            {
                list.add(cur.val);  //如果当前结点没有孩子结点或者孩子节点都已被访问过 
                s.pop();
                pre=cur; 
            }
            else
            {
                if(cur.right!=null)
                    s.push(cur.right);
                if(cur.left!=null)    
                    s.push(cur.left);
            }
        }    
    }

6.递归后序遍历

    /*递归后序遍历*/
    public ArrayList<Integer> postorderTraversal(TreeNode root) {
        if(root!=null){
            postorderTraversal(root.left);
            postorderTraversal(root.right);
            list.add(root.val);
        }
       return list;
    }

7.层次遍历

    /*层次遍历*/
    public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
        ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
        Queue<TreeNode> q = new LinkedList<TreeNode>();
        if(root!=null)
            q.add(root);
        while(!q.isEmpty()){
            ArrayList<TreeNode> inlist = new ArrayList<TreeNode>();
            ArrayList<Integer> result = new ArrayList<Integer>();
            while(!q.isEmpty()){
                inlist.add(q.poll());
            }
            for(int i=0;i<inlist.size();i++){
                result.add(inlist.get(i).val);
                if(inlist.get(i).left!=null)
                    q.offer(inlist.get(i).left);
                if(inlist.get(i).right!=null)
                    q.offer(inlist.get(i).right);
            }
            list.add(result);
        }
        return list;
    }

8.锯齿形层次遍历

    /*锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行)*/
    public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) {
        // write your code here
         ArrayList<ArrayList<Integer>> arr = new ArrayList<ArrayList<Integer>>();
        boolean direction = true;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        if(root!=null)
            stack.push(root);
        while(!stack.isEmpty()){
            ArrayList<Integer> result = new ArrayList<Integer>();
            List<TreeNode> list = new ArrayList<TreeNode>();
            while(!stack.isEmpty()){
                list.add(stack.pop());
            }
            for(int i=0;i<list.size();i++){
                result.add(list.get(i).val);
                if(direction){
                    if(list.get(i).left!=null)
                        stack.push(list.get(i).left);
                    if(list.get(i).right!=null)
                        stack.push(list.get(i).right);
                }
                else{
                    if(list.get(i).right!=null)
                        stack.push(list.get(i).right);
                    if(list.get(i).left!=null)
                        stack.push(list.get(i).left);
                }
            }
            if(direction)
                direction = false;
            else
                direction = true;
            arr.add(result);
        }
        return arr;
    }

9.倒序层次遍历

    /*倒序层次遍历*/
    public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) {
        // write your code here
        ArrayList<ArrayList<Integer>> arr = new ArrayList<ArrayList<Integer>>();
        Queue<TreeNode> q = new LinkedList<TreeNode>();
        if(root!=null)
            q.offer(root);
        while(!q.isEmpty()){
            ArrayList<TreeNode> list = new ArrayList<TreeNode>();
            ArrayList<Integer> result = new ArrayList<Integer>();

            while(!q.isEmpty()){
                list.add(q.poll());    
            }
            for(int i=0;i<list.size();i++){    
                result.add(list.get(i).val);
                if(list.get(i).left!=null){
                    q.offer(list.get(i).left);
                }
                if(list.get(i).right!=null){
                    q.offer(list.get(i).right);
                }
            }
            arr.add(0,result);
        }    
        return arr;
    }

10.二叉树深度

    /*二叉树深度*/
    public int depth(TreeNode root)   //树的深度    
    {    
        if(root == null)    
            return 0;    
        int d1,d2;    
        d1=depth(root.left);    
        d2=depth(root.right);    
        return (d1>d2?d1:d2)+1;    
    }

11.二叉树节点数

/*二叉树节点数*/
public int CountNode(TreeNode root)    
    {    
        if(root == null)    
            return 0;    
        return 1+CountNode(root.left)+CountNode(root.right);    
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值