zoukankan      html  css  js  c++  java
  • 【算法】二叉树前中后序的递归+迭代(java代码)

    【递归前中后序】
    // 前序遍历·递归·LC144_二叉树的前序遍历
    class Solution {
        ArrayList<Integer> preOrderReverse(TreeNode root) {
            ArrayList<Integer> result = new ArrayList<Integer>();
            preOrder(root, result);
            return result;
        }
     
        void preOrder(TreeNode root, ArrayList<Integer> result) {
            if (root == null) {
                return;
            }
            result.add(root.val);           // 注意这一句
            preOrder(root.left, result);
            preOrder(root.right, result);
        }
    }
    // 中序遍历·递归·LC94_二叉树的中序遍历
    class Solution {
        public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            inorder(root, res);
            return res;
        }
     
        void inorder(TreeNode root, List<Integer> list) {
            if (root == null) {
                return;
            }
            inorder(root.left, list);
            list.add(root.val);             // 注意这一句
            inorder(root.right, list);
        }
    }
    // 后序遍历·递归·LC145_二叉树的后序遍历
    class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            postorder(root, res);
            return res;
        }
     
        void postorder(TreeNode root, List<Integer> list) {
            if (root == null) {
                return;
            }
            postorder(root.left, list);
            postorder(root.right, list);
            list.add(root.val);             // 注意这一句
        }
    }
     
    【迭代前中后序】
    // 前序遍历顺序:中-左-右,入栈顺序:中-右-左
    class Solution {
        public List<Integer> preorderTraversal(TreeNode root) {
            List<Integer> result = new ArrayList<>();
            if (root == null){
                return result;
            }
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root); //
            while (!stack.isEmpty()){
                TreeNode node = stack.pop(); //
                result.add(node.val);
                if (node.right != null){
                    stack.push(node.right); //
                }
                if (node.left != null){
                    stack.push(node.left); //
                }
            }
            return result;
        }
    }
     
    // 中序遍历顺序: 左-中-右 入栈顺序: 左-右
    class Solution {
        public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> result = new ArrayList<>();
            if (root == null){
                return result;
            }
            Stack<TreeNode> stack = new Stack<>();
            TreeNode cur = root;
            while (cur != null || !stack.isEmpty()){
               if (cur != null){
                   stack.push(cur);
                   cur = cur.left; //
               }else{
                   cur = stack.pop();  // 左、中
                   result.add(cur.val);
                   cur = cur.right; //
               }
            }
            return result;
        }
    }
     
    // 后序遍历顺序 左-右-中 入栈顺序:中-左-右 出栈顺序:中-右-左, 最后翻转结果
    class Solution { 
        public List<Integer> postorderTraversal(TreeNode root) {
            List<Integer> result = new ArrayList<>();
            if (root == null){
                return result;
            }
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);  //
            while (!stack.isEmpty()){
                TreeNode node = stack.pop(); //
                result.add(node.val);
                if (node.left != null){
                    stack.push(node.left); //
                }
                if (node.right != null){
                    stack.push(node.right); //
                }
            }
            Collections.reverse(result);
            return result;
        }
    }
    class Solution {
        List<Integer> res;
        public List<Integer> postorderTraversal(TreeNode root) {
            // 迭代
            Deque<TreeNode> stack = new LinkedList<TreeNode>();
            List<Integer> res = new ArrayList<>();
            TreeNode prev = null; // 表示右子树是否已经遍历完成
            while (root != null || !stack.isEmpty()) {
                while (root != null) {
                    stack.push(root);
                    root = root.left;
                }
                root = stack.pop();
                // 处理右
                if (root.right == null || root.right == prev) {
                    res.add(root.val);
                    prev = root;
                    root = null;
                } else {
                    stack.push(root);
                    root = root.right;
                }
            }
            return res;
        }
    }
     
  • 相关阅读:
    团队冲刺(二)个人工作总结6
    团队冲刺(二)个人工作总结4
    网页开发--03(wampserver安装服务无法启动的问题)
    网页开发--02(开发环境配置)
    网页开发--01(常识)
    Three.js学习笔记05
    Three.js学习笔记04--纹理
    Three.js学习笔记03--光
    Three.js学习笔记02
    Three.js学习笔记01
  • 原文地址:https://www.cnblogs.com/coder-ydq/p/15013948.html
Copyright © 2011-2022 走看看