zoukankan      html  css  js  c++  java
  • 二叉树的前、中、后、层序遍历(递归、非递归)

    一、二叉树的前序遍历[LeetCode 144]

    1. 递归遍历

    public void preOrder(TreeNode root){
        if(root == null){
            return;
        }
        System.out.println(root.val);
        preOrder(root.left);
        preOrder(root.right);
    }
    

    2. 非递归遍历

    public void preOrder(TreeNode root){       
        LinkedList<TreeNode> stack = new LinkedList<>();
        stack.push(root);
        while(!stack.isEmpty()){
            if((root = stack.pop()) != null){
             	System.out.println(root.val);
                stack.push(root.right);
                stack.push(root.left);
            }        
        }
    }
    

    二、二叉树的中序遍历[LeetCode 94]

    1. 递归遍历

    public void inOrder(TreeNode root){
        if(root == null){
            return;
        }
        inOrder(root.left);
        System.out.println(root.val);
        inOrder(root.right);
    }
    

    2. 非递归遍历

    //栈顶结点为待中序遍历的结点。其余结点均为其根节点到栈顶结点路径上的结点。
    //只有栈顶结点可能为 null。
    public void inOrder(TreeNode root){
        LinkedList<TreeNode> stack = new LinkedList<>();
        stack.push(root);
        while(!stack.isEmpty()){
            if((root = stack.peek()) != null){
                stack.push(root.left);
            }
            else{
                stack.pop();
                if(!stack.isEmpty()){
                    root = stack.pop();
                    System.out.println(root.val);
                    stack.push(root.right);
                }                        
            }
        }
    }
    

    三、二叉树的后序遍历 [LeetCode 145]

    1. 递归遍历

    public void postOrder(TreeNode root){
        if(root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.println(root.val);
    }
    

    2. 非递归遍历1

    /*
    	栈顶结点为待后序遍历的结点。其余结点均为其根节点到栈顶结点路径上的结点。
    	只有栈顶结点可能为 null。
    	栈顶结点为null时,表示栈顶结点的后序遍历结束时,pre = 出栈。此时需要判断 pre是否为栈顶结点root的右子(此种判断的优点是当左子为空,右子为空时,不需要访问右子,直接访问父结点)。当pre为root的右子时,持续出栈,直至栈为空,或者pre为root的左子。
    */
    public void postOrder(TreeNode root){
        TreeNode pre;
        LinkedList<TreeNode> stack = new LinkedList<>();
        stack.push(root);
        while(!stack.isEmpty()){
            if((root = stack.peek()) != null){            
                stack.push(root.left);
            }
            else{
                pre = stack.pop();
                root = stack.peek();
                while(!stack.isEmpty() && pre == (root = stack.pop()).right){
                    pre = stack.pop();
                    System.out.println(pre.val);
                }
                if(!stack.isEmpty()){
                    stack.push(root.right);
                }            
            }
        }
    }
    

    3. 非递归遍历2

    public List<Integer> postorderTraversal(TreeNode root) {
        TreeNode pre = null;
        boolean forward = true;
        List<Integer> res = new LinkedList<>();
        LinkedList<TreeNode> stack = new LinkedList<>();
        stack.push(root);
        while(!stack.isEmpty()){
            if(forward){
                if((root = stack.peek()) != null){            
                    stack.push(root.left);
                }
                else{
                    forward = false;
                    pre = stack.pop();
                }
            }            
            else if(pre == (root = stack.peek()).right){                               
                pre = stack.pop();
                res.add(pre.val);
            }
            else{
                forward = true;
                stack.push(root.right);
            }                        
        }
        return res;
    }
    

    四、二叉树的层序遍历[LeetCode 102]

    public List<List<Integer>> levelOrder(TreeNode root) {    
        List<Integer> list = null;
        List<List<Integer>> res = new LinkedList<>();
        if(root == null){
            return res;
        }
        Queue<TreeNode> first = new LinkedList<>(), second = new LinkedList<>(), temp;
        first.offer(root);
        while(!first.isEmpty()){
            list = new LinkedList<>();
            while(!first.isEmpty()){
                root = first.poll();
                list.add(root.val);
                if(root.left != null){
                	second.offer(root.left);    
                }
                if(root.right != null){
                	second.offer(root.right);    
                }               
            }
            res.add(list);
            temp = first;
            first = second;
            second = temp;    
        }
        return res;
    }
    
  • 相关阅读:
    Linux systemtap定位系统IO资源使用情况(ok)
    DISK 100% BUSY,谁造成的?(ok)
    容易被误读的IOSTAT
    利用BLKTRACE分析IO性能
    iowait 过高问题的查找及解决linux
    top后台执行显示:top: failed tty get 错误
    Nginx解读内置非默认模块 ngx_http_stub_status_module
    Nginx健康检查模块
    平滑升级你的Nginx
    Practice telephone techniques
  • 原文地址:https://www.cnblogs.com/echie/p/9605360.html
Copyright © 2011-2022 走看看