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

来源:互联网 发布:seo全套视频教程 编辑:程序博客网 时间:2024/05/01 13:10

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

二叉树遍历方法

 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);        }
原创粉丝点击