zoukankan      html  css  js  c++  java
  • 二叉树常见遍历算法

    这几天在复习关于树的各种算法,做了一些题,也搜索了网上各种算法,现在来总结一下树的各种常见算法。
    本文涵盖:

    二叉树先中后序遍历(递归&非递归)算法

    层次遍历(正序&逆序&锯齿形)非递归算法

    二叉树深度算法

    结点总数算法

    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);    
        }
  • 相关阅读:
    分布式数据库管理系统
    Java并发(一)Java并发/多线程教程
    nginx重启后,反向代理失败之问题排查记录
    从spring源码汲取营养:模仿spring事件发布机制,解耦业务代码
    Mybatis中多表关联时,怎么利用association优雅写resultMap来映射vo
    曹工杂谈:为什么很少需要改Spring源码,因为扩展点太多了,说说Spring的后置处理器
    fastjson自由:controller上指定active profile,让你想序列化什么字段就序列化什么字段
    就因为加了Lombok的@Accessors(chain = true),bean拷贝工具类不干活了
    宽带爱折腾-将家里光猫转成桥接模式
    修改springfox-swagger源码,使example中时间格式默认为“yyyy-MM-dd HH:mm:ss”
  • 原文地址:https://www.cnblogs.com/yfsmooth/p/4671903.html
Copyright © 2011-2022 走看看