• 二叉树的前、中、后、层序遍历(递归、非递归)


    一、二叉树的前序遍历[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;
    }
    
  • 相关阅读:
    在vue项目中如何关闭Eslint校验
    npm安装依赖过程中报错:Error: Can't find Python executable "python", you can set the PYTHON env variable
    如何在SAE搭建属于自己的黑盒xss安全测试平台
    掌握下面常用函数,学php不再难
    解决ThinkPHP中开启调试模式无法加载模块的问题。
    生成规则:Q+年后两位+月+日卡券类型+八位字母和数字随机
    SPI的全名为Service Provider Interface
    03、第三个阶段,Java 核心技术
    02、第二个阶段,Java 基础入门
    01、第一个阶段,环境和工具准备
  • 原文地址:https://www.cnblogs.com/echie/p/9605360.html
Copyright © 2020-2023  润新知