• 二叉树各种遍历的递归/迭代写法


    一.二叉树的前序遍历

    https://leetcode-cn.com/problems/binary-tree-preorder-traversal/

    class Solution {
        public List<Integer> preorderTraversal(TreeNode root) {
            List<Integer> ans = new ArrayList<>();
            Stack<TreeNode> stack = new Stack<>();
            while (null != root || !stack.empty()) {
                while (null != root) {
                    ans.add(root.val);
                    stack.push(root);
                    root = root.left;
                }
                TreeNode node = stack.pop();
                root = node.right;
            }
            return ans;
        }
    }
    迭代
    class Solution {
    
        List<Integer> ans = null;
    
        public List<Integer> preorderTraversal(TreeNode root) {
            ans = new ArrayList<>();
            dfs(root);
            return ans;
        }
    
        //根左右
        void dfs(TreeNode root) {
            if (null == root)
                return;
            ans.add(root.val);
            dfs(root.left);
            dfs(root.right);
        }
    
    }
    递归

    二.二叉树的中序遍历

    https://leetcode-cn.com/problems/binary-tree-inorder-traversal/

    class Solution {
        public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> ans = new ArrayList<>();
            Stack<TreeNode> stack = new Stack<>();
            while (null != root || !stack.empty()) {
                while (null != root) {
                    stack.push(root);
                    root = root.left;
                }
                TreeNode node = stack.pop();
                ans.add(node.val);
                root = node.right;
            }
            return ans;
        }
    }
    迭代
    class Solution {
    
        List<Integer> ans = null;
    
        public List<Integer> inorderTraversal(TreeNode root) {
            ans = new ArrayList<>();
            dfs(root);
            return ans;
        }
    
        void dfs(TreeNode root) {
            if (null == root)
                return;
            dfs(root.left);
            ans.add(root.val);
            dfs(root.right);
        }
    
    }
    递归

    三.二叉树的后序遍历

    https://leetcode-cn.com/problems/binary-tree-postorder-traversal/

    class Solution {
    
        List<Integer> ans = null;
    
        public List<Integer> postorderTraversal(TreeNode root) {
            ans = new ArrayList<>();
            dfs(root);
            return ans;
        }
    
        void dfs(TreeNode root) {
            if (null == root)
                return;
            dfs(root.left);
            dfs(root.right);
            ans.add(root.val);
        }
    
    }
    递归
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
            List<Integer> ans = new ArrayList<>();
            Stack<TreeNode> stack = new Stack<>();
            TreeNode pre = null;
            while (null != root || !stack.empty()) {
                while (null != root) {
                    stack.push(root);
                    root = root.left;
                }
                TreeNode node = stack.pop();
                if (node.right == null || node.right == pre) {
                    ans.add(node.val);
                    pre = node;
                } else {
                    stack.push(node);
                    root = node.right;
                }
            }
            return ans;
        }
    }
    迭代

    四.二叉树的层次遍历

    https://leetcode-cn.com/problems/binary-tree-level-order-traversal/

    class Solution {
        public List<List<Integer>> levelOrder(TreeNode root) {
            List<List<Integer>> ans = new ArrayList<>();
            if (null == root)
                return ans;
            Queue<TreeNode> Q = new LinkedList<>();
            Q.add(root);
            while (!Q.isEmpty()) {
                int size = Q.size();
                List<Integer> temp = new ArrayList<>();
                for (int i = 0; i < size; i++) {
                    TreeNode node = Q.poll();
                    temp.add(node.val);
                    if (null != node.left)
                        Q.add(node.left);
                    if (null != node.right)
                        Q.add(node.right);
                }
                ans.add(temp);
            }
            return ans;
        }
    }
    迭代

    五.二叉树的锯齿形层次遍历

    https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/

    class Solution {
        public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
            List<List<Integer>> ans = new ArrayList<>();
            if (null == root)
                return ans;
            Queue<TreeNode> Q = new LinkedList<>();
            Stack<Integer> stack = new Stack<>();
            int flag = 1;
            Q.add(root);
            while (!Q.isEmpty()) {
                int size = Q.size();
                List<Integer> temp = new ArrayList<>();
                for (int i = 0; i < size; i++) {
                    TreeNode node = Q.poll();
                    if (null != node.left)
                        Q.add(node.left);
                    if (null != node.right)
                        Q.add(node.right);
                    if (flag == 1)
                        temp.add(node.val);
                    else
                        stack.push(node.val);
                }
                if (flag == 1)
                    ans.add(temp);
                else {
                    while (!stack.empty()) 
                        temp.add(stack.pop());
                    ans.add(temp);
                }
                flag *= -1;
            }
            return ans;
        }
    }
    迭代
     
  • 相关阅读:
    http协议
    web及时通信传输的几种方法
    头像上传可预览实现代码
    JavaScript对象基础知识总结
    js异步流程的promise及generator
    数据整理
    前端技术分类整理
    HTML随笔
    前端内网穿透,localtunnel
    CSS记录
  • 原文地址:https://www.cnblogs.com/yuanweidao/p/14605067.html
Copyright © 2020-2023  润新知