• 二叉树常见遍历算法


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

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

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

    二叉树深度算法

    结点总数算法

    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);    
        }
  • 相关阅读:
    How to create Ubuntu Image with Linaro Image Tools
    linux little tool
    Firefly-RK3288
    tensorflow 出现KeyError: 'tulips\8689672277_b289909f97_n.jpg'报错
    开发中涉及的框架与工具
    这三年被分布式坑惨了,曝光十大坑 【转载】
    C#动态编译及执行代码
    cron表达式可视化编辑页面
    AVPacket相关函数介绍
    ffmpeg 重要函数介绍
  • 原文地址:https://www.cnblogs.com/yfsmooth/p/4671903.html
Copyright © 2020-2023  润新知